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

SSR Output includes files which are only relevant for prerendering #220

Closed
alexanderniebuhr opened this issue Apr 2, 2024 · 0 comments · Fixed by #222
Closed

SSR Output includes files which are only relevant for prerendering #220

alexanderniebuhr opened this issue Apr 2, 2024 · 0 comments · Fixed by #222
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) pkg: cloudflare upstream

Comments

@alexanderniebuhr
Copy link
Member

alexanderniebuhr commented Apr 2, 2024

The SSR output of astro build, which is located in dist/_worker.js directory, contains files which are not used in SSR and only used for prerendering locally. We should not keep them and upload them to Cloudflare.

The goal is:

  • not to add a second bundling step
  • keep the SSR file chunks the same they are after running astro build
  • making sure unused files & chunks (because they are only needed for prerendering) are removed
  • if necessary put this under an experimental flag for the adapters options

Potential important parts of the code:

vite: {
// load .wasm files as WebAssembly modules
plugins: [
wasmModuleLoader({
disabled: !args?.wasmModuleImports,
}),
],
},

'astro:build:setup': ({ vite, target }) => {
if (target === 'server') {
vite.resolve ||= {};
vite.resolve.alias ||= {};
const aliases = [
{
find: 'react-dom/server',
replacement: 'react-dom/server.browser',
},
];
if (Array.isArray(vite.resolve.alias)) {
vite.resolve.alias = [...vite.resolve.alias, ...aliases];
} else {
for (const alias of aliases) {
(vite.resolve.alias as Record<string, string>)[alias.find] = alias.replacement;
}
}
vite.resolve.conditions ||= [];
// We need those conditions, previous these conditions where applied at the esbuild step which we removed
// https://github.com/withastro/astro/pull/7092
vite.resolve.conditions.push('workerd', 'worker');
vite.ssr ||= {};
vite.ssr.target = 'webworker';
vite.ssr.noExternal = true;
if (typeof _config.vite.ssr?.external === 'undefined') vite.ssr.external = [];
if (typeof _config.vite.ssr?.external === 'boolean')
vite.ssr.external = _config.vite.ssr?.external;
if (Array.isArray(_config.vite.ssr?.external))
// `@astrojs/vue` sets `@vue/server-renderer` to external
// https://github.com/withastro/astro/blob/e648c5575a8774af739231cfa9fc27a32086aa5f/packages/integrations/vue/src/index.ts#L119
// the cloudflare adapter needs to get all dependencies inlined, we use `noExternal` for that, but any `external` config overrides that
// therefore we need to remove `@vue/server-renderer` from the external config again
vite.ssr.external = _config.vite.ssr?.external.filter(
(entry) => entry !== '@vue/server-renderer'
);
vite.build ||= {};
vite.build.rollupOptions ||= {};
vite.build.rollupOptions.output ||= {};
// @ts-expect-error
vite.build.rollupOptions.output.banner ||=
'globalThis.process ??= {}; globalThis.process.env ??= {};';
vite.build.rollupOptions.external = _config.vite.build?.rollupOptions?.external ?? [];
// Cloudflare env is only available per request. This isn't feasible for code that access env vars
// in a global way, so we shim their access as `process.env.*`. This is not the recommended way for users to access environment variables. But we'll add this for compatibility for chosen variables. Mainly to support `@astrojs/db`
vite.define = {
'process.env': 'process.env',
...vite.define,
};
}
// we thought that vite config inside `if (target === 'server')` would not apply for client
// but it seems like the same `vite` reference is used for both
// so we need to reset the previous conflicting setting
// in the future we should look into a more robust solution
if (target === 'client') {
vite.resolve ||= {};
vite.resolve.conditions ||= [];
vite.resolve.conditions = vite.resolve.conditions.filter(
(c) => c !== 'workerd' && c !== 'worker'
);
}
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) pkg: cloudflare upstream
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants