mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Prevents automatic trailingSlash behavior with getStaticPaths (#4265)
* Prevents automatic trailingSlash behavior with getStaticPaths * Testing * weird change
This commit is contained in:
parent
c218100684
commit
8f845ca950
6 changed files with 60 additions and 3 deletions
5
.changeset/fluffy-otters-guess.md
Normal file
5
.changeset/fluffy-otters-guess.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Prevents automatic trailingSlash appending on getStaticPaths produced pages
|
|
@ -15,7 +15,7 @@ test.afterEach(async () => {
|
|||
|
||||
test.describe('Recursive elements with Solid', () => {
|
||||
test('Counter', async ({ astro, page }) => {
|
||||
await page.goto(astro.resolveUrl('/'));
|
||||
await page.goto('/');
|
||||
|
||||
const wrapper = page.locator('#case1');
|
||||
await expect(wrapper, 'component is visible').toBeVisible();
|
||||
|
|
|
@ -245,7 +245,7 @@ interface GeneratePathOptions {
|
|||
}
|
||||
|
||||
function addPageName(pathname: string, opts: StaticBuildOptions): void {
|
||||
opts.pageNames.push(pathname.replace(/\/?$/, '/').replace(/^\//, ''));
|
||||
opts.pageNames.push(pathname.replace(/^\//, ''));
|
||||
}
|
||||
|
||||
async function generatePath(
|
||||
|
|
|
@ -28,7 +28,11 @@ export function getRouteGenerator(
|
|||
})
|
||||
.join('');
|
||||
|
||||
const trailing = addTrailingSlash !== 'never' && segments.length ? '/' : '';
|
||||
// Unless trailingSlash config is set to 'always', don't automatically append it.
|
||||
let trailing: '/' | '' = '';
|
||||
if(addTrailingSlash === 'always' && segments.length) {
|
||||
trailing = '/';
|
||||
}
|
||||
const toPath = compile(template + trailing);
|
||||
return toPath;
|
||||
}
|
||||
|
|
|
@ -150,3 +150,25 @@ describe('getStaticPaths - numeric route params', () => {
|
|||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('getStaticPaths - Astro.url', () => {
|
||||
/** @type {import('./test-utils').Fixture} */
|
||||
let fixture;
|
||||
before(async () => {
|
||||
// reset the flag used by [...calledTwiceTest].astro between each test
|
||||
globalThis.isCalledOnce = false;
|
||||
|
||||
fixture = await loadFixture({
|
||||
root: './fixtures/astro-get-static-paths/',
|
||||
site: 'https://mysite.dev/',
|
||||
});
|
||||
await fixture.build();
|
||||
});
|
||||
|
||||
it('Sets the current pathname', async () => {
|
||||
const html = await fixture.readFile('/food/tacos/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
expect($('#url').text()).to.equal('/food/tacos');
|
||||
});
|
||||
});
|
||||
|
|
26
packages/astro/test/fixtures/astro-get-static-paths/src/pages/food/[name].astro
vendored
Normal file
26
packages/astro/test/fixtures/astro-get-static-paths/src/pages/food/[name].astro
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
---
|
||||
export async function getStaticPaths() {
|
||||
return [
|
||||
{
|
||||
params: { name: 'tacos' },
|
||||
},
|
||||
{
|
||||
params: { name: 'potatoes' },
|
||||
},
|
||||
{
|
||||
params: { name: 'spaghetti' }
|
||||
}
|
||||
]
|
||||
}
|
||||
---
|
||||
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<title>Food</title>
|
||||
</head>
|
||||
<body>
|
||||
<p id="url">{ Astro.url.pathname }</p>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue