0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-10 23:01:26 -05:00

Make Astro.url in format: 'preserve' match dev (#11191)

* Make Astro.url in format: 'preserve' match dev

* Create sweet-trainers-eat.md

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
This commit is contained in:
Matthew Phillips 2024-06-06 09:03:36 -04:00 committed by GitHub
parent 58b10a0731
commit 6e29a172f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 52 additions and 3 deletions

View file

@ -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

View file

@ -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;

View file

@ -1,8 +1,12 @@
---
const url = Astro.url;
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<h2>{url.pathname}</h2>
</body>
</html>

View file

@ -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 () => {
@ -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 () => {