Skip to content
Open
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/few-heads-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv-router': patch
---

Add basename in anchors href
6 changes: 4 additions & 2 deletions src/create-router.svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function navigate(path, options = {}) {

path = constructPath(path, options.params);
if (base.name === '#') {
path = new URL(path).hash;
path = new URL(path.replace('/#', '')).hash;
} else if (options.hash && !options.hash.startsWith('#')) {
options.hash = '#' + options.hash;
}
Expand Down Expand Up @@ -200,8 +200,10 @@ export async function onNavigate(path, options = {}) {
url.hash = options.hash || '';
if (base.name === '#') {
url.hash = path;
} else if (base.name && !path.startsWith(base.name)) {
url.pathname = join(base.name, path);
} else {
url.pathname = base.name ? join(base.name, path) : path;
url.pathname = path;
}
const historyMethod = options.replace ? 'replaceState' : 'pushState';
globalThis.history[historyMethod](options.state || {}, '', url.toString());
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/is-active.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function compare(compareFn, pathname, params) {

if (params) {
if (base.name === '#') {
return compareFn(location.pathname, new URL(constructPath(pathname, params)).hash.slice(1));
return compareFn(location.pathname, constructPath(pathname, params).replace('/#', ''));
} else {
return compareFn(location.pathname, constructPath(pathname, params));
}
Expand Down
9 changes: 4 additions & 5 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ export function constructPath(path, params) {
}

if (base.name === '#') {
const url = new URL(globalThis.location.toString());
url.hash = path;
url.search = '';

return url.toString();
if (path === '/') {
return '/#/';
}
return join('#', path);
}

return path;
Expand Down
18 changes: 14 additions & 4 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,37 @@ describe('constructPath (hash-based)', () => {

it('should return the original path when no params are provided', () => {
const result = constructPath('/posts');
expect(result).toBe('http://localhost:3000/#/posts');
expect(result).toBe('/#/posts');
});

it('should replace a single param in the path', () => {
const result = constructPath('/posts/:id', { id: '123' });
expect(result).toBe('http://localhost:3000/#/posts/123');
expect(result).toBe('/#/posts/123');
});

it('should replace multiple params in the path', () => {
const result = constructPath('/posts/:id/comments/:commentId', { id: '123', commentId: '456' });
expect(result).toBe('http://localhost:3000/#/posts/123/comments/456');
expect(result).toBe('/#/posts/123/comments/456');
});
});

describe('constructUrl', () => {
it('should create a path with search and hash', () => {
base.name = undefined;
const result = constructUrl('/posts', {
search: { q: 'test' },
hash: 'hash',
});
expect(result).toBe('http://localhost:3000/#/posts?q=test#hash');
expect(result).toBe('/posts?q=test#hash');
});

it('should create a path with search and hash (hash-based)', () => {
base.name = '#';
const result = constructUrl('/posts', {
search: { q: 'test' },
hash: 'hash',
});
expect(result).toBe('/#/posts?q=test#hash');
});
});

Expand Down