Skip to content
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

feat(ssr-streaming): Allow building without index.html during streaming-ssr #9387

Merged
merged 6 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/cli/src/commands/buildHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,17 @@ export const handler = async ({
)
}

console.log('Creating 200.html...')
// Streaming SSR does not use the index.html file.
if (!getConfig().experimental?.streamingSsr?.enabled) {
console.log('Creating 200.html...')

const indexHtmlPath = path.join(getPaths().web.dist, 'index.html')
const indexHtmlPath = path.join(getPaths().web.dist, 'index.html')

fs.copyFileSync(
indexHtmlPath,
path.join(getPaths().web.dist, '200.html')
)
fs.copyFileSync(
indexHtmlPath,
path.join(getPaths().web.dist, '200.html')
)
}
},
},
].filter(Boolean)
Expand Down
9 changes: 2 additions & 7 deletions packages/vite/src/buildFeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,8 @@ export const buildFeServer = async ({ verbose, webDir }: BuildOptions = {}) => {
configFile: viteConfigPath,
build: {
outDir: rwPaths.web.distServer,
ssr: true, // use boolean, and supply the inputs in rollup options (see Documentation)
rollupOptions: {
input: {
'entry.server': rwPaths.web.entryServer,
Document: rwPaths.web.document, // We need the document for React's fallback
},
},
ssr: true, // use boolean here, instead of string.
// rollup inputs are defined in the vite plugin
},
envFile: false,
logLevel: verbose ? 'info' : 'warn',
Expand Down
33 changes: 33 additions & 0 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync } from 'fs'
import path from 'path'

import react from '@vitejs/plugin-react'
import type { InputOption } from 'rollup'
import type { ConfigEnv, UserConfig, PluginOption } from 'vite'
import { normalizePath } from 'vite'

Expand Down Expand Up @@ -240,6 +241,9 @@ export default function redwoodPluginVite(): PluginOption[] {
emptyOutDir: true,
manifest: !env.ssrBuild ? 'build-manifest.json' : undefined,
sourcemap: !env.ssrBuild && rwConfig.web.sourceMap, // Note that this can be boolean or 'inline'
rollupOptions: {
input: getRollupInput(!!env.ssrBuild),
},
},
legacy: {
buildSsrCjsExternalHeuristics: rwConfig.experimental?.rsc?.enabled
Expand Down Expand Up @@ -281,3 +285,32 @@ export default function redwoodPluginVite(): PluginOption[] {
}),
]
}

/**
*
* This function configures how vite (actually Rollup) will bundle.
*
* By default, the entry point is the index.html file - even if you don't specify it in RollupOptions
*
* With streaming SSR, out entrypoint is different - either entry.client.tsx or entry.server.tsx
* and the html file is not used at all, because it is defined in Document.tsx
*
* @param ssr {boolean} Whether to return the SSR inputs or not
* @returns Rollup input Options
*/
function getRollupInput(ssr: boolean): InputOption | undefined {
const rwConfig = getConfig()
const rwPaths = getPaths()

// @NOTE once streaming ssr is out of experimental, this will become the default
if (rwConfig.experimental.streamingSsr.enabled) {
return ssr
? {
'entry.server': rwPaths.web.entryServer as string,
Document: rwPaths.web.document, // We need the document for React's fallback
}
: (rwPaths.web.entryClient as string)
}

return rwPaths.web.html
}