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

Allow %-encoding in route filenames #5056

Merged
merged 8 commits into from
Jul 8, 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/strong-swans-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] Allow %-encoded filenames
24 changes: 24 additions & 0 deletions documentation/docs/02-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,27 @@ src/routes/[a].js
src/routes/[b].svelte
src/routes/[...catchall].svelte
```

#### Encoding

Filenames are URI-decoded, meaning that (for example) a filename like `%40[username].svelte` would match characters beginning with `@`:

```js
// @filename: ambient.d.ts
declare global {
const assert: {
equal: (a: any, b: any) => boolean;
};
}

export {};

// @filename: index.js
// ---cut---
assert.equal(
decodeURIComponent('%40[username].svelte'),
'@[username].svelte'
);
```

To express a `%` character, use `%25`, otherwise the result will be malformed.
4 changes: 2 additions & 2 deletions packages/kit/src/vite/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export async function build_server(options, client) {

if (file) {
const resolved = path.resolve(cwd, file);
const relative = path.relative(config.kit.files.routes, resolved);
const relative = decodeURIComponent(path.relative(config.kit.files.routes, resolved));
const name = posixify(path.join('entries/endpoints', relative.replace(/\.js$/, '')));
input[name] = resolved;
}
Expand All @@ -151,7 +151,7 @@ export async function build_server(options, client) {
// ...and every component used by pages...
manifest_data.components.forEach((file) => {
const resolved = path.resolve(cwd, file);
const relative = path.relative(config.kit.files.routes, resolved);
const relative = decodeURIComponent(path.relative(config.kit.files.routes, resolved));

const name = relative.startsWith('..')
? posixify(path.join('entries/fallbacks', path.basename(file)))
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function kit() {
// their location in the source code, which is helpful for debugging
manifest_data.components.forEach((file) => {
const resolved = path.resolve(cwd, file);
const relative = path.relative(svelte_config.kit.files.routes, resolved);
const relative = decodeURIComponent(path.relative(svelte_config.kit.files.routes, resolved));

const name = relative.startsWith('..')
? path.basename(file)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { page } from '$app/stores';
</script>

<h1>${$page.params.ticker}</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { page } from '$app/stores';
</script>

<h1>@{$page.params.username}</h1>
2 changes: 2 additions & 0 deletions packages/kit/test/apps/basics/src/routes/encoded/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
<a href="/encoded/土豆">土豆</a>
<a href="/encoded/反应">反应</a>
<a href="/encoded/redirect">Redirect</a>
<a href="/encoded/@svelte">@svelte</a>
<a href="/encoded/$SVLT">$SVLT</a>
12 changes: 12 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,18 @@ test.describe('Encoded paths', () => {
const response = await request.get('/encoded/endpoint');
expect(response.headers()['content-type']).toBe('application/json; charset=utf-8');
});

test('allows %-encoded characters in directory names', async ({ page, clicknav }) => {
await page.goto('/encoded');
await clicknav('[href="/encoded/$SVLT"]');
expect(await page.textContent('h1')).toBe('$SVLT');
});

test('allows %-encoded characters in filenames', async ({ page, clicknav }) => {
await page.goto('/encoded');
await clicknav('[href="/encoded/@svelte"]');
expect(await page.textContent('h1')).toBe('@svelte');
});
});

test.describe('Errors', () => {
Expand Down