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

[fix] urlhash in page store #3273

Merged
merged 4 commits into from
Jan 10, 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/heavy-moons-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

url hash is now properly reflected in page store
3 changes: 3 additions & 0 deletions packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ export class Renderer {

let error_args;

// url.hash is empty when coming from the server
url.hash = window.location.hash;

try {
for (let i = 0; i < nodes.length; i += 1) {
const is_leaf = i === nodes.length - 1;
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ export class Router {
// Call `pushState` to add url to history so going back works.
// Also make a delay, otherwise the browser default behaviour would not kick in
setTimeout(() => history.pushState({}, '', url.href));
const info = this.parse(url);
if (info) {
return this.renderer.update(info, [], false);
}
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
import { page } from '$app/stores';
import { onMount } from 'svelte';

/** @type {string} */
let curWindowHash;
onMount(() => setHashVar());
function setHashVar() {
curWindowHash = window.location.hash;
}
</script>

<svelte:window on:hashchange={setHashVar} />

<h1 id="window-hash">{curWindowHash}</h1>
<h1 id="page-url-hash">{$page.url.hash}</h1>

<a href="#target">Nav to #ing with anchor tag</a>
<div id="target">Target</div>
19 changes: 19 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,25 @@ test.describe.parallel('Routing', () => {
).toBe('rgb(255, 0, 0)');
});

test('$page.url.hash is correctly set on page load', async ({ page, javaScriptEnabled }) => {
if (javaScriptEnabled) {
await page.goto('/routing/hashes/pagestore#target');
expect(await page.textContent('#window-hash')).toBe('#target');
expect(await page.textContent('#page-url-hash')).toBe('#target');
}
});

test('$page.url.hash is correctly set on navigation', async ({ page, javaScriptEnabled }) => {
if (javaScriptEnabled) {
await page.goto('/routing/hashes/pagestore');
expect(await page.textContent('#window-hash')).toBe('');
expect(await page.textContent('#page-url-hash')).toBe('');
await page.click('[href="#target"]');
expect(await page.textContent('#window-hash')).toBe('#target');
expect(await page.textContent('#page-url-hash')).toBe('#target');
}
});

test('fallthrough', async ({ page }) => {
await page.goto('/routing/fallthrough-simple/invalid');
expect(await page.textContent('h1')).toBe('Page');
Expand Down