Fix inlined hoisted scripts and SSR (#3593)

* Fix inlined hoisted scripts and SSR

* Adds a changeset
This commit is contained in:
Matthew Phillips 2022-06-14 15:14:15 -04:00 committed by GitHub
parent 56a99bebbe
commit 0e2314d8e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes uses of inline hoisted scripts in SSR

View file

@ -80,7 +80,7 @@ export class App {
const links = createLinkStylesheetElementSet(info.links, manifest.site); const links = createLinkStylesheetElementSet(info.links, manifest.site);
const filteredScripts = info.scripts.filter( const filteredScripts = info.scripts.filter(
(script) => typeof script !== 'string' && script?.stage !== 'head-inline' (script) => typeof script === 'string' || script?.stage !== 'head-inline'
) as string[]; ) as string[];
const scripts = createModuleScriptElementWithSrcSet(filteredScripts, manifest.site); const scripts = createModuleScriptElementWithSrcSet(filteredScripts, manifest.site);

View file

@ -0,0 +1,9 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<script>console.log('hello world');</script>
</body>
</html>

View file

@ -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://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);
});
});