Skip to content

Commit

Permalink
Fix inlined hoisted scripts and SSR (#3593)
Browse files Browse the repository at this point in the history
* Fix inlined hoisted scripts and SSR

* Adds a changeset
  • Loading branch information
matthewp committed Jun 14, 2022
1 parent 56a99be commit 0e2314d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/loud-bikes-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes uses of inline hoisted scripts in SSR
2 changes: 1 addition & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class App {
const links = createLinkStylesheetElementSet(info.links, manifest.site);

const filteredScripts = info.scripts.filter(
(script) => typeof script !== 'string' && script?.stage !== 'head-inline'
(script) => typeof script === 'string' || script?.stage !== 'head-inline'
) as string[];
const scripts = createModuleScriptElementWithSrcSet(filteredScripts, manifest.site);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<script>console.log('hello world');</script>
</body>
</html>
34 changes: 34 additions & 0 deletions packages/astro/test/ssr-hoisted-script.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';

describe('Hoisted scripts in SSR', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-hoisted-script/',
experimental: {
ssr: true,
},
adapter: testAdapter(),
});
await fixture.build();
});

async function fetchHTML(path) {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http:https://example.com' + path);
const response = await app.render(request);
const html = await response.text();
return html;
}

it('Inlined scripts get included', async () => {
const html = await fetchHTML('/');
const $ = cheerioLoad(html);
expect($('script').length).to.equal(1);
});
});

0 comments on commit 0e2314d

Please sign in to comment.