Skip to content

Commit

Permalink
feat: add more options for reloadNuxtApp
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 16, 2023
1 parent 676c93c commit 86be1b2
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions packages/nuxt/src/app/composables/chunk.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
export interface ReloadNuxtAppOptions {
/**
* Number of milliseconds in which to ignore future reload requests
*
* @default {10000}
*/
ttl?: number
/**
* Force a reload even if one has occurred within the previously specified TTL.
*
* @default {false}
*/
force?: boolean
/**
* The path to reload. If this is different from the current window location it will
* trigger a navigation and add an entry in the browser history.
*
* @default {window.location.pathname}
*/
path?: string
}

export function reloadNuxtApp (options?: ReloadNuxtAppOptions) {
const path = options?.path || window.location.pathname
export function reloadNuxtApp (options: ReloadNuxtAppOptions = {}) {
const path = options.path || window.location.pathname

let handledPath: Record<string, any> = {}
try {
handledPath = JSON.parse(localStorage.getItem('nuxt:reload') || '{}')
} catch {}

if (handledPath?.path !== path || handledPath?.expires < Date.now()) {
localStorage.setItem('nuxt:reload', JSON.stringify({ path, expires: Date.now() + 10000 }))
if (options.force || handledPath?.path !== path || handledPath?.expires < Date.now()) {
localStorage.setItem('nuxt:reload', JSON.stringify({ path, expires: Date.now() + (options.ttl ?? 10000) }))
if (window.location.pathname !== path) {
window.location.href = path
} else {
Expand Down

0 comments on commit 86be1b2

Please sign in to comment.