0
Fork 0
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:
Matthew Phillips 2022-08-12 12:19:32 -04:00 committed by GitHub
parent c218100684
commit 8f845ca950
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Prevents automatic trailingSlash appending on getStaticPaths produced pages

View file

@ -15,7 +15,7 @@ test.afterEach(async () => {
test.describe('Recursive elements with Solid', () => { test.describe('Recursive elements with Solid', () => {
test('Counter', async ({ astro, page }) => { test('Counter', async ({ astro, page }) => {
await page.goto(astro.resolveUrl('/')); await page.goto('/');
const wrapper = page.locator('#case1'); const wrapper = page.locator('#case1');
await expect(wrapper, 'component is visible').toBeVisible(); await expect(wrapper, 'component is visible').toBeVisible();

View file

@ -245,7 +245,7 @@ interface GeneratePathOptions {
} }
function addPageName(pathname: string, opts: StaticBuildOptions): void { function addPageName(pathname: string, opts: StaticBuildOptions): void {
opts.pageNames.push(pathname.replace(/\/?$/, '/').replace(/^\//, '')); opts.pageNames.push(pathname.replace(/^\//, ''));
} }
async function generatePath( async function generatePath(

View file

@ -28,7 +28,11 @@ export function getRouteGenerator(
}) })
.join(''); .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); const toPath = compile(template + trailing);
return toPath; return toPath;
} }

View file

@ -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');
});
});

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