Skip to content
Open
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-preserve-url-pathname.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes `Astro.url.pathname` for non-index pages when using `build.format: 'preserve'`. Previously, a page like `src/pages/about-me.astro` would output to `dist/about-me.html` but `Astro.url.pathname` would incorrectly return `/about-me/` instead of `/about-me.html`.
13 changes: 9 additions & 4 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export async function renderPath({
config.build.format,
config.trailingSlash,
route.type,
route.isIndex,
);

const request = createRequest({
Expand Down Expand Up @@ -580,6 +581,7 @@ function getUrlForPath(
format: AstroConfig['build']['format'],
trailingSlash: AstroConfig['trailingSlash'],
routeType: RouteType,
isIndex: boolean,
): URL {
/**
* Examples:
Expand All @@ -588,9 +590,12 @@ function getUrlForPath(
*/

let ending: string;
switch (format) {
case 'directory':
case 'preserve': {
// For `preserve`, non-index routes output as flat `.html` files (like `file`),
// while index routes output as `dir/index.html` (like `directory`).
const effectiveFormat =
format === 'preserve' ? (isIndex ? 'directory' : 'file') : format;
switch (effectiveFormat) {
case 'directory': {
ending = trailingSlash === 'never' ? '' : '/';
break;
}
Expand All @@ -602,7 +607,7 @@ function getUrlForPath(
}
let buildPathname: string;
if (pathname === '/' || pathname === '') {
if (format === 'file') {
if (effectiveFormat === 'file') {
buildPathname = joinPaths(base, 'index.html');
} else {
buildPathname = collapseDuplicateTrailingSlashes(base + ending, trailingSlash !== 'never');
Expand Down
33 changes: 32 additions & 1 deletion packages/astro/test/page-format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@ describe('build.format', () => {
});
});

describe('preserve', () => {
let fixture: Fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/page-format/',
build: {
format: 'preserve',
},
outDir: './dist/page-format-preserve/',
});
});

describe('Build', () => {
before(async () => {
await fixture.build();
});

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

it('Astro.url pathname has trailing slash for index pages', async () => {
let html = await fixture.readFile('/nested/index.html');
let $ = cheerio.load(html);
assert.equal($('h2').text(), '/nested/');
});
});
});

describe('preserve - i18n', () => {
let fixture: Fixture;
before(async () => {
Expand Down Expand Up @@ -120,7 +151,7 @@ describe('build.format', () => {
it('relative urls created point to sibling folders', async () => {
let html = await fixture.readFile('/en/nested/page.html');
let $ = cheerio.load(html);
assert.equal($('#another').attr('href'), '/test/en/nested/page/another/');
assert.equal($('#another').attr('href'), '/test/en/nested/another/');
});

it('index files are written as index.html', async () => {
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading