Skip to content
Merged
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/fix-before-hydration-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Emit the `before-hydration` script chunk for the `client` Vite environment. The chunk was only emitted for `prerender` and `ssr` environments, causing a 404 when browsers tried to load it. This broke hydration for any integration using `injectScript('before-hydration', ...)`, including Lit SSR.
5 changes: 5 additions & 0 deletions .changeset/small-jeans-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix `Astro.url.pathname` for the root page when using `build.format: "file"` so it resolves to `/index.html` instead of `/.html` during builds.
6 changes: 5 additions & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,11 @@ function getUrlForPath(
}
let buildPathname: string;
if (pathname === '/' || pathname === '') {
buildPathname = collapseDuplicateTrailingSlashes(base + ending, trailingSlash !== 'never');
if (format === 'file') {
buildPathname = joinPaths(base, 'index.html');
} else {
buildPathname = collapseDuplicateTrailingSlashes(base + ending, trailingSlash !== 'never');
}
} else if (routeType === 'endpoint') {
const buildPathRelative = removeLeadingForwardSlash(pathname);
buildPathname = joinPaths(base, buildPathRelative);
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/vite-plugin-scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export default function astroScriptsPlugin({ settings }: { settings: AstroSettin
const hasHydrationScripts = settings.scripts.some((s) => s.stage === 'before-hydration');
if (
hasHydrationScripts &&
(this.environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.prerender ||
(this.environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.client ||
this.environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.prerender ||
this.environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.ssr)
) {
this.emitFile({
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/before-hydration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ describe('Astro Scripts before-hydration', () => {
let $ = cheerio.load(html);
assert.equal($('astro-island[before-hydration-url]').length, 1);
});

it('Emits the before-hydration chunk to the client output', async () => {
let html = await fixture.readFile('/index.html');
let $ = cheerio.load(html);
let url = $('astro-island').attr('before-hydration-url');
assert.ok(url, 'before-hydration-url attribute should have a value');
// The URL should point to a file that exists in the build output
let content = await fixture.readFile(url);
assert.ok(content, 'before-hydration chunk should exist in the client build output');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
---
const url = Astro.url;
---
<html>
<head><title>testing</title></head>
<body><h1>testing</h1></body>
<body>
<h1>testing</h1>
<h2 id="url">{url.pathname}</h2>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
const another = new URL('./another/', Astro.url);
const url = Astro.url;
---
<a id="another" href={another.pathname}></a>
<p id="url">{url.pathname}</p>
12 changes: 12 additions & 0 deletions packages/astro/test/page-format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ describe('build.format', () => {
await fixture.build();
});

it('Astro.url pathname includes /index.html for root page in build', async () => {
let html = await fixture.readFile('/index.html');
let $ = cheerio.load(html);
assert.equal($('#url').text(), '/index.html');
});

it('Astro.url pathname includes .html for non-root pages in build', async () => {
let html = await fixture.readFile('/nested/page.html');
let $ = cheerio.load(html);
assert.equal($('#url').text(), '/nested/page.html');
});

it('relative urls created point to sibling folders', async () => {
let html = await fixture.readFile('/nested/page.html');
let $ = cheerio.load(html);
Expand Down
Loading