Skip to content

Commit

Permalink
fix: inject script for ssr mode (#3339)
Browse files Browse the repository at this point in the history
* fix: inject script for ssr mode

* chore: changeset
  • Loading branch information
thepassle authored May 11, 2022
1 parent 71f852c commit 3dc68e1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-adults-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

fixes injectscript in ssr mode
14 changes: 13 additions & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'> & {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
}
Expand Down

0 comments on commit 3dc68e1

Please sign in to comment.