Skip to content

Commit

Permalink
Fix location in deno v1.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yandeu committed Jan 22, 2021
1 parent de44c8a commit 1e19866
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/components/router.ts
@@ -1,4 +1,6 @@
// inspired by https://codesandbox.io/s/build-own-react-router-v4-mpslz
declare const isSSR: boolean
declare const _nano: any

import { Component } from '../component'
import { FC, h, _render } from '../core'
Expand Down Expand Up @@ -79,7 +81,7 @@ export class Switch extends Component {

this.props.children.forEach((child: any) => {
const { path, exact } = child.props
const match = matchPath(window.location.pathname, { path, exact })
const match = matchPath(isSSR ? _nano.location.pathname : window.location.pathname, { path, exact })
if (match) {
// if there is already a matched component, we do not match *
if (component && path === '*') return
Expand Down
16 changes: 6 additions & 10 deletions src/ssr.ts
@@ -1,4 +1,5 @@
declare const isSSR: boolean
declare const _nano: any

import { render } from './core'
import { _state } from './state'
Expand All @@ -13,17 +14,12 @@ const detectSSR = () => {
// @ts-ignore
globalThis.isSSR = detectSSR() === true ? true : undefined

export const initSSR = (pathname: string = '/') => {
// @ts-ignore
const isDeno = typeof Deno !== 'undefined'
// @ts-ignore
globalThis._nano = { isSSR, location: { pathname: '/' } }

if (!isDeno)
// @ts-ignore
globalThis.window = isSSR ? { location: { pathname } } : window
else {
// @ts-ignore
window.location = { pathname }
}
export const initSSR = (pathname: string = '/') => {
// set pathname
_nano.location = { pathname }

// @ts-ignore
globalThis.document = isSSR ? new DocumentSSR() : window.document
Expand Down

0 comments on commit 1e19866

Please sign in to comment.