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

Better browser support for updating urls #41

Merged
merged 7 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions src/utilities/createRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Resolved, Route, RouteMethods, Routes } from '@/types'
import { createRouteMethods, resolveRoutes } from '@/utilities'
import { resolveRoutesRegex } from '@/utilities/resolveRoutesRegex'
import { routeMatch } from '@/utilities/routeMatch'
import { updateWindowLocation } from '@/utilities/updateWindowLocation'
import { updateBrowserUrl } from '@/utilities/updateBrowserUrl'

type RouterPush = (url: string, options?: { replace: boolean }) => Promise<void>
type RouterReplace = (url: string) => Promise<void>
Expand Down Expand Up @@ -32,7 +32,7 @@ export function createRouter<T extends Routes>(routes: T): Router<T> {
const match = routeMatch(resolvedWithRegex, url)

if (!match) {
return updateWindowLocation(url, options)
return updateBrowserUrl(url, options)
}

throw 'not implemented'
Expand Down
6 changes: 6 additions & 0 deletions src/utilities/isBrowser.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, test } from 'vitest'
import { isBrowser } from '@/utilities/isBrowser'

test('isBrowser returns true when environment is browser', () => {
expect(isBrowser).toBe(true)
})
6 changes: 6 additions & 0 deletions src/utilities/isBrowser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, test } from 'vitest'
import { isBrowser } from '@/utilities/isBrowser'

test('isBrowser returns false when environment is node', () => {
expect(isBrowser).toBe(false)
})
2 changes: 2 additions & 0 deletions src/utilities/isBrowser.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggesting this as an alternative to the globalExists pattern. Thinking avoiding eval is probably a good idea for a public repository.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
export const isBrowser: boolean = typeof window !== 'undefined' && typeof window.document !== 'undefined'
pleek91 marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions src/utilities/updateBrowserUrl.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import { expect, test, vi } from 'vitest'
import { updateBrowserUrl } from '@/utilities/updateBrowserUrl'

test('updates the window location', () => {
vi.spyOn(window.location, 'assign').mockImplementation(() => {})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these mockImplementation calls make sure the actual location isn't updated. I tried a few different attempts to keep the location changes isolated to each test but this was the only thing I tried that worked.


updateBrowserUrl('http://example.com2/foo')

expect(window.location.assign).toHaveBeenCalled()
})

test('updates the window location using replace when options.replace is true', () => {
vi.spyOn(window.location, 'replace').mockImplementation(() => {})

updateBrowserUrl('http://example.com/foo', { replace: true })

expect(window.location.replace).toHaveBeenCalled()
})

test('updates the history', () => {
vi.spyOn(history, 'pushState').mockImplementation(() => {})

updateBrowserUrl('/foo')

expect(history.pushState).toHaveBeenCalled()
})

test('updates the history using replaceState when options.replace is true', () => {
vi.spyOn(history, 'replaceState').mockImplementation(() => {})

updateBrowserUrl('/foo', { replace: true })

expect(history.replaceState).toHaveBeenCalled()
})
9 changes: 9 additions & 0 deletions src/utilities/updateBrowserUrl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest'
import { isBrowser } from '@/utilities/isBrowser'
import { updateBrowserUrl } from '@/utilities/updateBrowserUrl'

test('does nothing when the window is not available', () => {
updateBrowserUrl('http://example.com2/foo')

expect(isBrowser).toBe(false)
})
38 changes: 38 additions & 0 deletions src/utilities/updateBrowserUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { isBrowser } from '@/utilities/isBrowser'

type UpdateBrowserUrlOptions = {
replace?: boolean,
}
pleek91 marked this conversation as resolved.
Show resolved Hide resolved
export function updateBrowserUrl(url: string, options: UpdateBrowserUrlOptions = {}): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on a popstate event where the browser is telling us the user navigated outside of our router's control, would you imagine we call this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if a popstate event is emitted the browser already has an updated url. So we wouldn't need to call this.

if (!isBrowser) {
return
}

if (isSameOrigin(url)) {
return updateHistory(url, options)
}

return updateWindow(url, options)
}

function updateHistory(url: string, options: UpdateBrowserUrlOptions = {}): void {
if (options.replace) {
return history.replaceState({}, '', url)
}

history.pushState({}, '', url)
}

function updateWindow(url: string, options: UpdateBrowserUrlOptions = {}): void {
if (options.replace) {
return window.location.replace(url)
}

return window.location.assign(url)
}

function isSameOrigin(url: string): boolean {
const { origin } = new URL(url, window.location.origin)

return origin === window.location.origin
}
pleek91 marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 0 additions & 20 deletions src/utilities/updateWindowLocation.spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/utilities/updateWindowLocation.ts

This file was deleted.

6 changes: 6 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { defineConfig } from 'vitest/config'
import dts from 'vite-plugin-dts'

export default defineConfig({
test: {
environmentMatchGlobs: [
['**\/*.browser.spec.ts', 'happy-dom'],
['**\/*.spec.ts', 'node'],
]
pleek91 marked this conversation as resolved.
Show resolved Hide resolved
},
resolve: {
alias: [
{
Expand Down