fix: inject script for ssr mode (#3339)

* fix: inject script for ssr mode

* chore: changeset
This commit is contained in:
Pascal Schilp 2022-05-11 16:53:53 +02:00 committed by GitHub
parent 71f852cad9
commit 3dc68e148e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
fixes injectscript in ssr mode

View file

@ -78,7 +78,19 @@ export class App {
const renderers = manifest.renderers;
const info = this.#routeDataToRouteInfo.get(routeData!)!;
const links = createLinkStylesheetElementSet(info.links, manifest.site);
const scripts = createModuleScriptElementWithSrcSet(info.scripts, manifest.site);
const filteredScripts = info.scripts.filter(script => typeof script !== 'string' && script?.stage !== 'head-inline') as string[];
const scripts = createModuleScriptElementWithSrcSet(filteredScripts, manifest.site);
// Add all injected scripts to the page.
for (const script of info.scripts) {
if (typeof script !== 'string' && script.stage === 'head-inline') {
scripts.add({
props: {},
children: script.children,
});
}
}
const result = await render({
links,

View file

@ -12,7 +12,7 @@ export interface RouteInfo {
routeData: RouteData;
file: string;
links: string[];
scripts: string[];
scripts: Array<string | {children: string, stage: string}>;
}
export type SerializedRouteInfo = Omit<RouteInfo, 'routeData'> & {

View file

@ -109,7 +109,7 @@ function buildManifest(
routes.push({
file: '',
links: Array.from(pageData.css),
scripts,
scripts: [...scripts, ...astroConfig._ctx.scripts.filter(script => script.stage === 'head-inline').map(({stage, content}) => ({stage, children: content}))],
routeData: serializeRouteData(pageData.route),
});
}