Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed May 8, 2024
1 parent 4030a69 commit 4ed3f5d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
9 changes: 6 additions & 3 deletions packages/react-router/lib/dom/ssr/single-fetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,12 @@ async function fetchAndDecode(url: URL, init?: RequestInit) {
let decoded = await decodeViaTurboStream(res.body, window);
return { status: res.status, data: decoded.value };
} catch (e) {
// Unfortunately I don't think we can get access to the response text here.
// Pre-emptively cloning seems incorrect and we can't clone after consuming
// the body via turbo-stream
// 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");
}
}
Expand Down
45 changes: 29 additions & 16 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,21 +1174,37 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => {
);
}

if (!ctx.reactRouterConfig.ssr) {
await handleSpaMode(
if (ctx.reactRouterConfig.prerender != null) {
// If we have prerender routes, that takes precedence over SPA mode
// which is ssr:false and only the rot route being rendered
await handlePrerender(
viteConfig,
ctx.reactRouterConfig,
serverBuildDirectory,
clientBuildDirectory
);
} else if (ctx.reactRouterConfig.prerender != null) {
await handlePrerender(
} else if (!ctx.reactRouterConfig.ssr) {
await handleSpaMode(
viteConfig,
ctx.reactRouterConfig,
serverBuildDirectory,
clientBuildDirectory
);
}

// For both SPA mode and prerendering, we can remove the server builds
// if ssr:false is set
if (!ctx.reactRouterConfig.ssr) {
// Cleanup - we no longer need the server build assets
viteConfig.logger.info(
[
"Removing the server build in",
colors.green(serverBuildDirectory),
"due to ssr:false",
].join(" ")
);
fse.removeSync(serverBuildDirectory);
}
},
},
async buildEnd() {
Expand Down Expand Up @@ -1642,10 +1658,10 @@ async function getRouteMetadata(
async function getPrerenderBuildAndHandler(
viteConfig: Vite.ResolvedConfig,
reactRouterConfig: Awaited<ReturnType<typeof resolveReactRouterConfig>>,
serverBuildDirectoryPath: string
serverBuildDirectory: string
) {
let serverBuildPath = path.join(
serverBuildDirectoryPath,
serverBuildDirectory,
reactRouterConfig.serverBuildFile
);
let build = await import(url.pathToFileURL(serverBuildPath).toString());
Expand All @@ -1661,13 +1677,13 @@ async function getPrerenderBuildAndHandler(
async function handleSpaMode(
viteConfig: Vite.ResolvedConfig,
reactRouterConfig: Awaited<ReturnType<typeof resolveReactRouterConfig>>,
serverBuildDirectoryPath: string,
serverBuildDirectory: string,
clientBuildDirectory: string
) {
let { handler } = await getPrerenderBuildAndHandler(
viteConfig,
reactRouterConfig,
serverBuildDirectoryPath
serverBuildDirectory
);
let request = new Request(`http://localhost${reactRouterConfig.basename}`);
let response = await handler(request);
Expand Down Expand Up @@ -1699,28 +1715,27 @@ async function handleSpaMode(
colors.bold(path.relative(process.cwd(), clientBuildDirectory)) +
" directory"
);

// Cleanup - we no longer need the server build assets
fse.removeSync(serverBuildDirectoryPath);
}

async function handlePrerender(
viteConfig: Vite.ResolvedConfig,
reactRouterConfig: Awaited<ReturnType<typeof resolveReactRouterConfig>>,
serverBuildDirectoryPath: string,
serverBuildDirectory: string,
clientBuildDirectory: string
) {
let { build, handler } = await getPrerenderBuildAndHandler(
viteConfig,
reactRouterConfig,
serverBuildDirectoryPath
serverBuildDirectory
);

let routes = createPrerenderRoutes(build.routes);
let routesToPrerender = reactRouterConfig.prerender || ["/"];
let requestInit = {
headers: {
"X-React-router-Prerender": "yes",
// Header that can be used in the loader to know if you're running at
// build time or runtime
"X-React-Router-Prerender": "yes",
},
};
for (let path of routesToPrerender) {
Expand All @@ -1737,7 +1752,6 @@ async function handlePrerender(
}
await prerenderRoute(
handler,
routes,
reactRouterConfig.basename,
path,
clientBuildDirectory,
Expand Down Expand Up @@ -1781,7 +1795,6 @@ async function handlePrerender(

async function prerenderRoute(
handler: RequestHandler,
routes: DataRouteObject[],
basename: string,
prerenderPath: string,
clientBuildDirectory: string,
Expand Down

0 comments on commit 4ed3f5d

Please sign in to comment.