Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevents automatic trailingSlash behavior with getStaticPaths #4265

Merged
merged 4 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fluffy-otters-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Prevents automatic trailingSlash appending on getStaticPaths produced pages
2 changes: 1 addition & 1 deletion packages/astro/e2e/solid-recurse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/core/routing/manifest/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
22 changes: 22 additions & 0 deletions packages/astro/test/astro-get-static-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Original file line number Diff line number Diff line change
@@ -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>