Skip to content

Commit

Permalink
feat(ssr-streaming): Allow building without index.html during streami…
Browse files Browse the repository at this point in the history
…ng-ssr (redwoodjs#9387)
  • Loading branch information
dac09 authored Nov 9, 2023
1 parent c4dedc6 commit 76e1672
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
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
}

0 comments on commit 76e1672

Please sign in to comment.