Last active
September 1, 2022 11:24
-
-
Save Kolobok12309/bee89f80e14814a4080fa5ba2aa2a7ad to your computer and use it in GitHub Desktop.
Nuxtjs webcache fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
... | |
buildModules: [ | |
[ // 1 | |
'@nuxtjs/router', | |
{ | |
path: 'configs', | |
keepDefaultRouter: true, | |
}, | |
], | |
], | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Router from 'vue-router'; | |
const cacheUrlMatchers = [ | |
{ | |
hostname: 'yandexwebcache.net', | |
pathname: '/yandbtm', | |
}, | |
{ | |
hostname: 'webcache.googleusercontent.com', | |
pathname: '/search', | |
}, | |
]; | |
// Check if fullUrl is cache resource | |
const isCachedUrl = (fullUrl = '') => { | |
if (process.server) return null; | |
const url = new URL(fullUrl); | |
return cacheUrlMatchers | |
.some(({ hostname, pathname }) => | |
url.hostname === hostname && url.pathname === pathname | |
); | |
}; | |
export function createRouter(ssrContext, createDefaultRouter, routerOptions, config) { | |
const defaultRouter = createDefaultRouter(ssrContext, config); | |
const options = routerOptions || defaultRouter.options; | |
// Check for current location is cache | |
const isCache = process.client && isCachedUrl(window.location.href); // 2 | |
// Change mode if we in cache, to prevent "Uncaught DOMException" | |
const mode = isCache // 3 | |
? 'abstract' | |
: options.mode; | |
const resultOptions = { | |
...options, | |
mode, | |
}; | |
const router = new Router(resultOptions); // 4 | |
// Set nuxt replaced methods | |
router.push = defaultRouter.push; | |
// Set init route in cache | |
if (isCache) { | |
const origRouterResolve = router.resolve.bind(router); | |
// Get fullpath of original page | |
const { route } = window.__NUXT__.state; // 5 | |
const { fullPath } = route; | |
// Mock first resolve call | |
router.resolve = () => { // 6 | |
// Unmock all others | |
router.resolve = origRouterResolve; | |
return origRouterResolve(fullPath); | |
}; | |
} | |
return router; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How it works? / Как это работает?
@nuxtjs/router
/ Мы создаем свой роутер с помощью@nuxtjs/router
abstract
, to prevent error while routing / Если приложение в кеше, меняем режим роутера наabstract
, дабы не было ошибки при переходахvue-router
push
for nuxt patched / Создать роутер и подменить дефолтныйvue-router
push
на пропатченный накстомfullPath
of current page, for example i usevuex-router-sync
and hasfullPath
inwindow.__NUXT__.state.route.fullPath
/ Если мы в кеше, получаем реальныйfullPath
текущей страницы, для примера я используюvuex-router-sync
и могу получитьfullPath
изwindow.__NUXT__.state.route.fullPath
router.resolve
to preventnuxt
usewindow.location
/ Мокаем первый вызовrouter.resolve
, чтобыnuxt
не использовалwindow.location