-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for prerendering #11539
Merged
+677
−78
Merged
Add support for prerendering #11539
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2f3c65c
Add support for prerendering
brophdawg11 d807140
Add changeset
brophdawg11 c3d26d2
Fix a few integration tests
brophdawg11 4fcd029
Decouple ssr and prerender configs
brophdawg11 722bb80
Integration tests
brophdawg11 99dab4d
Updates
brophdawg11 4030a69
Fix isDeepFrozen integration test check to handle null values
brophdawg11 4ed3f5d
Minor updates
brophdawg11 5bf9019
Update remix-serve to put proper headers on pre-rendered .data files
brophdawg11 db09881
Add a few basic integration tests
brophdawg11 ce4c8b1
Add a few more prerender e2e tests
brophdawg11 fd6493a
Merge branch 'v7' into brophdawg11/prerender
brophdawg11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
"react-router": minor | ||
--- | ||
|
||
- Add support for `prerender` config in the React Router vite plugin, to support existing SSG use-cases | ||
- You can use the `prerender` config to pre-render your `.html` and `.data` files at build time and then serve them statically at runtime (either from a running server or a CDN) | ||
- `prerender` can either be an array of string paths, or a function (sync or async) that returns an array of strings so that you can dynamically generate the paths by talking to your CMS, etc. | ||
|
||
```ts | ||
export default defineConfig({ | ||
plugins: [ | ||
reactRouter({ | ||
// Single fetch is required for prerendering (which will be the default in v7) | ||
future: { | ||
unstable_singleFetch: true, | ||
}, | ||
async prerender() { | ||
let slugs = await fakeGetSlugsFromCms(); | ||
// Prerender these paths into `.html` files at build time, and `.data` | ||
// files if they have loaders | ||
return ["/", "/about", ...slugs.map((slug) => `/product/${slug}`)]; | ||
}, | ||
}), | ||
tsconfigPaths(), | ||
], | ||
}); | ||
|
||
async function fakeGetSlugsFromCms() { | ||
await new Promise((r) => setTimeout(r, 1000)); | ||
return ["shirt", "hat"]; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,16 +294,19 @@ export function singleFetchUrl(reqUrl: URL | string) { | |
|
||
async function fetchAndDecode(url: URL, init?: RequestInit) { | ||
let res = await fetch(url, init); | ||
if (res.headers.get("Content-Type")?.includes("text/x-turbo")) { | ||
invariant(res.body, "No response body to decode"); | ||
invariant(res.body, "No response body to decode"); | ||
try { | ||
let decoded = await decodeViaTurboStream(res.body, window); | ||
return { status: res.status, data: decoded.value }; | ||
} catch (e) { | ||
// Can't clone after consuming the body via turbo-stream so we can't | ||
// include the body here. In an ideal world we'd look for a turbo-stream | ||
// content type here, or even X-Remix-Response but then folks can't | ||
// statically deploy their prerendered .data files to a CDN unless they can | ||
// tell that CDN to add special headers to those certain files - which is a | ||
// bit restrictive. | ||
throw new Error("Unable to decode turbo-stream response"); | ||
} | ||
Comment on lines
+309
to
316
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is why we can't surface the |
||
|
||
// If we didn't get back a turbo-stream response, then we never reached the | ||
// Remix server and likely this is a network error - just expose up the | ||
// response body as an Error | ||
throw new Error(await res.text()); | ||
} | ||
|
||
// Note: If you change this function please change the corresponding | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See below, but we can't surface this underlying CDN error any longer with pre-rendering :/