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

Preserve hash when computing path from url #44

Merged
merged 3 commits into from
Feb 12, 2025
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/rude-boats-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chialab/synapse": patch
---

Preserve hash when computing path from url
3 changes: 3 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ jobs:
- name: Install project dependencies
run: yarn install

- name: Install browsers
run: yarn playwright install

- name: Run tests
run: yarn test:browser --coverage

Expand Down
2 changes: 1 addition & 1 deletion src/Router/Pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class Pattern {
* @returns False if does not match, grouped values if it does.
*/
matches(path: string): RequestParams | false {
path = path.split('?')[0];
path = path.split('#')[0].split('?')[0];
if (this.cache[path]) {
return this.cache[path];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
async navigate(
path: Path | string,
init?: RequestInit,
data: any = null,

Check warning on line 316 in src/Router/Router.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
trigger = true,
force = false,
parentRequest?: Request,
Expand Down Expand Up @@ -380,7 +380,7 @@
async replace(
path: Path | string,
init?: RequestInit,
data: any = null,

Check warning on line 383 in src/Router/Router.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
trigger = true,
parentRequest?: Request,
parentResponse?: Response
Expand Down Expand Up @@ -588,7 +588,7 @@
return null;
}
const path = new Path(pathname.replace(this.base, ''));
return `${path.pathname}${path.search}`;
return `${path.pathname}${path.search}${path.hash?.length > 1 ? path.hash : ''}`;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/Router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ describe('Router', () => {
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('/base/')).toBe('/');
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('/base/?query')).toBe('/?query');
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('/base/path')).toBe('/path');
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('/base/path#hash')).toBe('/path#hash');
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('/base/path?query')).toBe(
'/path?query'
);
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('/base/path?query#hash')).toBe(
'/path?query#hash'
);
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('http://local/base')).toBe('/');
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('http://local/wrong')).toBeNull();
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('http://wrong/base')).toBeNull();
expect(new Router({ origin: 'http://local', base: '/base' }).pathFromUrl('http://wrong')).toBeNull();
expect(new Router({ origin: 'http://local', base: '/base#' }).pathFromUrl('/base#/hash')).toBe('/hash');
expect(new Router({ origin: 'http://local', base: '/base#' }).pathFromUrl('/base#/hash?query')).toBe(
'/hash?query'
);
});

test('should resolve a path', () => {
Expand Down