Skip to content

Commit 2d8959d

Browse files
committed
client/router: create uid instead of displayName
1 parent 0cc1ed0 commit 2d8959d

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

client/next.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ const Component = evalScript(component).default
1313

1414
const router = new Router({ Component, props })
1515
const container = document.getElementById('__next')
16-
const appProps = { Component, props, router: {} }
16+
const appProps = { Component, props, router }
1717

1818
render(createElement(App, { ...appProps }), container)

client/router.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ export default class Router {
66
constructor (initialData) {
77
this.subscriptions = []
88

9-
const { Component } = initialData
10-
const { pathname } = location
11-
const route = toRoute(pathname)
9+
const id = createUid()
10+
const route = toRoute(location.pathname)
1211

1312
this.currentRoute = route
14-
this.currentComponent = Component.displayName
15-
this.currentComponentData = initialData
13+
this.currentComponentData = { ...initialData, id }
1614

1715
// set up the component cache (by route keys)
1816
this.components = { [route]: initialData }
1917

2018
// in order for `e.state` to work on the `onpopstate` event
2119
// we have to register the initial route upon initialization
22-
const url = pathname + (location.search || '') + (location.hash || '')
23-
this.replace(Component, url)
20+
this.replace(id, getURL())
2421

2522
this.onPopState = this.onPopState.bind(this)
2623
window.addEventListener('unload', () => {})
@@ -30,8 +27,7 @@ export default class Router {
3027
onPopState (e) {
3128
this.abortComponentLoad()
3229
const cur = this.currentComponent
33-
const pathname = location.pathname
34-
const url = pathname + (location.search || '') + (location.hash || '')
30+
const url = getURL()
3531
const { fromComponent, route } = e.state || {}
3632
if (fromComponent && cur && fromComponent === cur) {
3733
// if the component has not changed due
@@ -47,7 +43,7 @@ export default class Router {
4743
// since the URL has already changed
4844
location.reload()
4945
} else {
50-
this.currentRoute = route || toRoute(pathname)
46+
this.currentRoute = route || toRoute(location.pathname)
5147
this.currentComponent = data.Component.displayName
5248
this.currentComponentData = data
5349
this.set(url)
@@ -88,40 +84,31 @@ export default class Router {
8884
this.change('pushState', fromComponent, url, fn)
8985
}
9086

91-
replace (fromComponent, url, fn) {
92-
this.change('replaceState', fromComponent, url, fn)
87+
replace (id, url, fn) {
88+
this.change('replaceState', id, url, fn)
9389
}
9490

95-
change (method, component, url, fn) {
91+
change (method, id, url, fn) {
9692
this.abortComponentLoad()
9793

98-
const set = (name) => {
99-
this.currentComponent = name
100-
const state = name
101-
? { fromComponent: name, route: this.currentRoute }
102-
: {}
94+
const set = (id) => {
95+
const state = id ? { fromComponent: id, route: this.currentRoute } : {}
10396
history[method](state, null, url)
10497
this.set(url)
10598
if (fn) fn(null)
10699
}
107100

108-
const componentName = component && component.displayName
109-
if (component && !componentName) {
110-
throw new Error('Initial component must have a unique `displayName`')
111-
}
112-
113-
if (this.currentComponent &&
114-
componentName !== this.currentComponent) {
101+
if (this.currentComponentData && id !== this.currentComponentData.id) {
115102
this.fetchComponent(url, (err, data) => {
116103
if (!err) {
117104
this.currentRoute = toRoute(url)
118105
this.currentComponentData = data
119-
set(data.Component.displayName)
106+
set(data.id)
120107
}
121108
if (fn) fn(err, data)
122109
})
123110
} else {
124-
set(componentName)
111+
set(id)
125112
}
126113
}
127114

@@ -139,7 +126,7 @@ export default class Router {
139126
}
140127

141128
fetchComponent (url, fn) {
142-
const pathname = parse(url, true)
129+
const { pathname } = parse(url)
143130
const route = toRoute(pathname)
144131

145132
let cancelled = false
@@ -174,11 +161,12 @@ export default class Router {
174161
if (err) {
175162
if (!cancelled) fn(err)
176163
} else {
164+
const d = { data, id: createUid() }
177165
// we update the cache even if cancelled
178166
if (!this.components[route]) {
179-
this.components[route] = data
167+
this.components[route] = d
180168
}
181-
if (!cancelled) fn(null, data)
169+
if (!cancelled) fn(null, d)
182170
}
183171
})
184172

@@ -224,6 +212,14 @@ export function loadComponent (url, fn) {
224212
})
225213
}
226214

215+
function getURL () {
216+
return location.pathname + (location.search || '') + (location.hash || '')
217+
}
218+
219+
function createUid () {
220+
return Math.floor(Math.random() * 1e16)
221+
}
222+
227223
function loadJSON (url, fn) {
228224
const xhr = new XMLHttpRequest()
229225
xhr.onload = () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy