Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 607dfe0

Browse files
committed
Move history functions
1 parent cd95d2c commit 607dfe0

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

runtime/src/app/app.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
import {
1414
HydratedTarget,
1515
Target,
16-
ScrollPosition,
1716
Redirect,
1817
Page
1918
} from './types';
@@ -71,15 +70,6 @@ export function set_target(element) {
7170
target = element;
7271
}
7372

74-
const _history = typeof history !== 'undefined' ? history : {
75-
pushState: (state: any, title: string, href: string) => {},
76-
replaceState: (state: any, title: string, href: string) => {},
77-
scrollRestoration: ''
78-
};
79-
export { _history as history };
80-
81-
export const scroll_history: Record<string, ScrollPosition> = {};
82-
8373
export function extract_query(search: string) {
8474
const query = Object.create(null);
8575
if (search.length > 0) {
@@ -152,13 +142,6 @@ export function handle_error(url: URL) {
152142
render([], props, { host, path: pathname, query, params: {} });
153143
}
154144

155-
export function scroll_state() {
156-
return {
157-
x: pageXOffset,
158-
y: pageYOffset
159-
};
160-
}
161-
162145
export async function handle_target(dest: Target) {
163146
if (root_component) stores.preloading.set(true);
164147

runtime/src/app/goto/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { history, select_target } from '../app';
1+
import { history } from '../start';
2+
import { select_target } from '../app';
23
import { navigate, cid } from '../start';
34

45
export default function goto(

runtime/src/app/start/index.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import {
22
handle_target,
3-
history,
43
initial_data,
5-
scroll_history,
6-
scroll_state,
74
select_target,
85
handle_error,
96
set_target
107
} from '../app';
118
import {
9+
ScrollPosition,
1210
Target
1311
} from '../types';
1412
import prefetch from '../prefetch/index';
@@ -23,24 +21,33 @@ export function set_cid(n: number) {
2321
cid = n;
2422
}
2523

24+
const _history = typeof history !== 'undefined' ? history : {
25+
pushState: (state: any, title: string, href: string) => {},
26+
replaceState: (state: any, title: string, href: string) => {},
27+
scrollRestoration: ''
28+
};
29+
export { _history as history };
30+
31+
export const scroll_history: Record<string, ScrollPosition> = {};
32+
2633
export default function start(opts: {
2734
target: Node
2835
}): Promise<void> {
29-
if ('scrollRestoration' in history) {
30-
history.scrollRestoration = 'manual';
36+
if ('scrollRestoration' in _history) {
37+
_history.scrollRestoration = 'manual';
3138
}
3239

3340
// Adopted from Nuxt.js
3441
// Reset scrollRestoration to auto when leaving page, allowing page reload
3542
// and back-navigation from other pages to use the browser to restore the
3643
// scrolling position.
3744
addEventListener('beforeunload', () => {
38-
history.scrollRestoration = 'auto';
45+
_history.scrollRestoration = 'auto';
3946
});
4047

4148
// Setting scrollRestoration to manual again when returning to this page.
4249
addEventListener('load', () => {
43-
history.scrollRestoration = 'manual';
50+
_history.scrollRestoration = 'manual';
4451
});
4552

4653
set_target(opts.target);
@@ -55,7 +62,7 @@ export default function start(opts: {
5562
return Promise.resolve().then(() => {
5663
const { hash, href } = location;
5764

58-
history.replaceState({ id: uid }, '', href);
65+
_history.replaceState({ id: uid }, '', href);
5966

6067
const url = new URL(location.href);
6168

@@ -122,7 +129,7 @@ function handle_click(event: MouseEvent) {
122129
const noscroll = a.hasAttribute('sapper:noscroll');
123130
navigate(target, null, noscroll, url.hash);
124131
event.preventDefault();
125-
history.pushState({ id: cid }, '', url.href);
132+
_history.pushState({ id: cid }, '', url.href);
126133
}
127134
}
128135

@@ -135,6 +142,13 @@ function find_anchor(node: Node) {
135142
return node;
136143
}
137144

145+
function scroll_state() {
146+
return {
147+
x: pageXOffset,
148+
y: pageYOffset
149+
};
150+
}
151+
138152
function handle_popstate(event: PopStateEvent) {
139153
scroll_history[cid] = scroll_state();
140154

@@ -151,7 +165,7 @@ function handle_popstate(event: PopStateEvent) {
151165
// hashchange
152166
set_uid(uid + 1);
153167
set_cid(uid);
154-
history.replaceState({ id: cid }, '', location.href);
168+
_history.replaceState({ id: cid }, '', location.href);
155169
}
156170
}
157171

0 commit comments

Comments
 (0)