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

fix(pwa): dynamic hi route and F5 #98

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion config/pwa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { appDescription, appName } from '../constants/index'
const scope = '/'

export const pwa: ModuleOptions = {
// for local build + start workbox messages
// mode: 'development',
registerType: 'autoUpdate',
scope,
base: scope,
Expand Down Expand Up @@ -36,10 +38,20 @@ export const pwa: ModuleOptions = {
},
workbox: {
globPatterns: ['**/*.{js,css,html,txt,png,ico,svg}'],
navigateFallbackDenylist: [/^\/api\//],
navigateFallbackDenylist: [/^\/api\//, /^\/hi\//],
navigateFallback: '/',
cleanupOutdatedCaches: true,
runtimeCaching: [
{
handler: 'NetworkFirst',
urlPattern: ({ sameOrigin, url }) => sameOrigin && url.pathname.startsWith('/hi/'),
options: {
plugins: [{
handlerDidError: async () => Response.redirect('/hi', 302),
cacheWillUpdate: async () => null,
}],
},
},
Comment on lines +45 to +54
Copy link
Owner

Choose a reason for hiding this comment

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

It it possible to identify dynamic routes automatically in the Nuxt module? This is a starter template after all, hard code those path isn't really helpful

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I'll try to add it to the module

Copy link
Contributor Author

Choose a reason for hiding this comment

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

{
urlPattern: /^https:\/\/fonts.googleapis.com\/.*/i,
handler: 'CacheFirst',
Expand Down
3 changes: 1 addition & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export default defineNuxtConfig({
},
prerender: {
crawlLinks: false,
routes: ['/'],
ignore: ['/hi'],
routes: ['/', '/hi'],
},
},

Expand Down
20 changes: 19 additions & 1 deletion pages/[...all].vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
<script setup lang="ts">
const router = useRouter()
const route = useRoute()
const online = useOnline()
const hiOffline = ref(false)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can switch this to message and change it in checkHiOnline


function checkHiOnline() {
hiOffline.value = !online.value && route.path.startsWith('/hi')
}

// eslint-disable-next-line n/prefer-global/process
if (process.client)
watch(() => [online.value, route], checkHiOnline, { immediate: true, flush: 'post' })

onBeforeMount(checkHiOnline)
</script>

<template>
<main p="x4 y10" text="center teal-700 dark:gray-200">
<div text-4xl>
<div i-carbon-warning inline-block />
</div>
<div>Not found</div>
<div v-if="hiOffline">
You're offline, don't refresh the page
</div>
<div v-else>
Not found
</div>
<div>
<button text-sm btn m="3 t8" @click="router.back()">
Back
Expand Down