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

Fix script handling in injectRoute shared entrypoint during SSR #12392

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/slimy-pets-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix issue where shared entrypoints in injectRoute() remove client-side scripts in SSR builds.
32 changes: 13 additions & 19 deletions packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { BuildOptions, Rollup, Plugin as VitePlugin } from 'vite';
import type { AstroSettings } from '../../../@types/astro.js';
import { viteID } from '../../util.js';
import type { BuildInternals } from '../internal.js';
import { getPageDataByViteID } from '../internal.js';
import { getPageDatasByHoistedScriptId } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin.js';
import type { StaticBuildOptions } from '../types.js';
import { shouldInlineAsset } from './util.js';
Expand All @@ -12,7 +11,7 @@
}

export function vitePluginHoistedScripts(
settings: AstroSettings,

Check notice on line 14 in packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts

View workflow job for this annotation

GitHub Actions / Lint

lint/correctness/noUnusedFunctionParameters

This parameter is unused.

Check notice on line 14 in packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts

View workflow job for this annotation

GitHub Actions / Lint

lint/correctness/noUnusedVariables

This parameter is unused.
internals: BuildInternals,
): VitePlugin {
let assetsInlineLimit: NonNullable<BuildOptions['assetsInlineLimit']>;
Expand Down Expand Up @@ -73,23 +72,18 @@
shouldInlineAsset(output.code, output.fileName, assetsInlineLimit);
let removeFromBundle = false;
const facadeId = output.facadeModuleId!;
const pages = internals.hoistedScriptIdToPagesMap.get(facadeId)!;
for (const pathname of pages) {
const vid = viteID(new URL('.' + pathname, settings.config.root));
const pageInfo = getPageDataByViteID(internals, vid);
if (pageInfo) {
if (canBeInlined) {
pageInfo.hoistedScript = {
type: 'inline',
value: output.code,
};
removeFromBundle = true;
} else {
pageInfo.hoistedScript = {
type: 'external',
value: id,
};
}
for (const pageData of getPageDatasByHoistedScriptId(internals, facadeId)) {
if (canBeInlined) {
pageData.hoistedScript = {
type: 'inline',
value: output.code,
};
removeFromBundle = true;
} else {
pageData.hoistedScript = {
type: 'external',
value: id,
};
}
}

Expand Down
Loading