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:
parent
58b10a0731
commit
6e29a172f1
4 changed files with 52 additions and 3 deletions
5
.changeset/sweet-trainers-eat.md
Normal file
5
.changeset/sweet-trainers-eat.md
Normal 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
|
|
@ -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;
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
---
|
||||
const url = Astro.url;
|
||||
---
|
||||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Testing</h1>
|
||||
<h2>{url.pathname}</h2>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
Loading…
Add table
Reference in a new issue