Skip to content

Commit

Permalink
Make Astro.url in format: 'preserve' match dev (#11191)
Browse files Browse the repository at this point in the history
* Make Astro.url in format: 'preserve' match dev

* Create sweet-trainers-eat.md

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
  • Loading branch information
matthewp and florian-lefebvre authored Jun 6, 2024
1 parent 58b10a0 commit 6e29a17
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-trainers-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a case where `Astro.url` would be incorrect when having `build.format` set to `'preserve'` in the Astro config
14 changes: 13 additions & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,19 @@ function getUrlForPath(
* pathname: /, /foo
* base: /
*/
const ending = format === 'directory' ? (trailingSlash === 'never' ? '' : '/') : '.html';

let ending: string;
switch(format) {
case 'directory':
case 'preserve': {
ending = trailingSlash === 'never' ? '' : '/';
break;
}
default: {
ending = '.html';
break;
}
}
let buildPathname: string;
if (pathname === '/' || pathname === '') {
buildPathname = base;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
const url = Astro.url;
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<h2>{url.pathname}</h2>
</body>
</html>
32 changes: 30 additions & 2 deletions packages/astro/test/page-format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,35 @@ describe('build.format', () => {
});
});

describe('preserve', () => {
describe('preserve - i18n', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
base: '/test',
root: './fixtures/page-format/',
trailingSlash: 'always',
build: {
format: 'preserve',
},
});
});

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

it('Astro.url points to right file', async () => {
let html = await fixture.readFile('/nested/index.html');
let $ = cheerio.load(html);
console.log(html);
assert.equal($('h2').text(), '/test/nested/');
});
});
});

describe('preserve - i18n', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
Expand Down Expand Up @@ -81,7 +109,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/another/');
assert.equal($('#another').attr('href'), '/test/en/nested/page/another/');
});

it('index files are written as index.html', async () => {
Expand Down

0 comments on commit 6e29a17

Please sign in to comment.