Skip to content

Commit 6709cf7

Browse files
fix(website): validate view and typescript query
1 parent d08fe90 commit 6709cf7

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

packages/overmind-website/src/overmind/effects.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,36 @@ export const storage = {
1818
}
1919

2020
export const router = (() => {
21+
const validViewQuery = ['react', 'vue', 'angular']
22+
const validTypescriptQuery = ['true', 'false']
2123
let currentPath
2224
let currentQuery: Query = {
2325
view: storage.get('theme'),
2426
typescript: storage.get('typescript'),
2527
}
2628
let currentHash = location.hash
2729

30+
function isValidQuery(query: Query) {
31+
return (
32+
validViewQuery.includes(query.view) &&
33+
validTypescriptQuery.includes(query.typescript)
34+
)
35+
}
36+
if (!isValidQuery(currentQuery)) {
37+
currentQuery.view = 'react'
38+
currentQuery.typescript = 'false'
39+
}
40+
2841
return {
2942
getPath() {
3043
return currentPath
3144
},
3245
route(url: string, action: (payload: RouteContext) => void) {
33-
page(url, ({ params, pathname, path, querystring }) => {
46+
page(url, ({ params, pathname, querystring }) => {
3447
// We want to preserve the query
35-
if (!querystring) {
48+
const query = queryString.parse(querystring)
49+
50+
if (!querystring || !isValidQuery(query)) {
3651
if (location.hash === currentHash) {
3752
currentHash = ''
3853
} else {
@@ -47,8 +62,6 @@ export const router = (() => {
4762
return
4863
}
4964

50-
const query = queryString.parse(querystring)
51-
5265
currentPath = pathname.split('?')[0]
5366
currentQuery = {
5467
view: query.view,

packages/overmind-website/src/utils.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ const Example: React.SFC<{ name: string }> = ({ name }) => {
9898

9999
useEffect(
100100
() => {
101+
let isUmountedOrChanged = false
101102
import('../examples/' + name).then((module) => {
103+
if (isUmountedOrChanged) {
104+
return
105+
}
106+
102107
const content = module.default(state.typescript, state.theme)
103108
if (!content) {
104109
return console.warn('Missing content for ' + getKey())
@@ -109,6 +114,10 @@ const Example: React.SFC<{ name: string }> = ({ name }) => {
109114
isLoading: false,
110115
})
111116
})
117+
118+
return () => {
119+
isUmountedOrChanged = true
120+
}
112121
},
113122
[name, state.theme, state.typescript]
114123
)

0 commit comments

Comments
 (0)