Skip to content

Instantly share code, notes, and snippets.

@rishi-raj-jain
Created June 17, 2022 16:00
Show Gist options
  • Save rishi-raj-jain/18fa3071231188db54b4e6e2d7de5cb8 to your computer and use it in GitHub Desktop.
Save rishi-raj-jain/18fa3071231188db54b4e6e2d7de5cb8 to your computer and use it in GitHub Desktop.
// This file was added by layer0 init.
// You should commit this file to source control.
require('dotenv').config()
const { Router, CustomCacheKey } = require('@layer0/core/router')
const { nuxtRoutes } = require('@layer0/nuxt')
const homeRedirect = require('./faas/home-redirect.js')
const clearCache = require('./faas/clear-cache.js')
const sitemap = require('./faas/sitemap.js')
const feed = require('./faas/feed.js')
const isCached = process.env.CACHE === 'true'
const isPreview = process.env.IS_PREVIEW === 'true'
const pagePrerendering = [
'/feed/uk',
'/feed/fr',
'/feed/it',
'/feed/es',
'/feed/de',
'/feed/us',
'/feed/au',
'/sitemap.xml',
'/api/products/all?lang=en&currency=GBP',
'/api/products/all?lang=en&currency=AUD',
'/api/products/all?lang=en&currency=USD',
'/api/products/all?lang=en&currency=EUR',
'/api/products/all?lang=fr&currency=EUR',
'/api/products/all?lang=de&currency=EUR',
'/api/products/all?lang=it&currency=EUR',
'/api/products/all?lang=es&currency=EUR',
]
const prerender = [
{ top: 25 },
...[...pagePrerendering].map(path => ({ path })),
]
const apiFallback = ({ removeUpstreamResponseHeader, proxy, cache }) => {
removeUpstreamResponseHeader('cache-control')
cache({
browser: {
maxAgeSeconds: 0,
},
edge: {
maxAgeSeconds: 60 * 60 * 24 * 365,
staleWhileRevalidateSeconds: 60 * 60 * 24,
},
})
proxy('someApi')
}
module.exports = new Router()
.requireBasicAuth({
username: process.env.BASIC_AUTH_USERNAME,
password: process.env.BASIC_AUTH_PASSWORD,
})
.prerender(prerender)
.match('/:path*', ({ cache, removeUpstreamResponseHeader }) => {
removeUpstreamResponseHeader('set-cookie')
cache({
browser: false,
edge: false,
});
})
.match('^((?!/cobrand-customer).)*$', ({ cache, setResponseHeader }) => {
if (isPreview) setResponseHeader('X-Robots-Tag', 'noindex');
setResponseHeader('X-Frame-Options', 'DENY');
setResponseHeader('X-Content-Type-Options', 'nosniff');
setResponseHeader('X-XSS-Protection', '1');
cache({
key: new CustomCacheKey().excludeAllQueryParametersExcept(
'sku',
'collar',
'lid',
'lang',
'currency',
'type',
'code',
'slug',
),
browser: false,
edge: isCached
? {
maxAgeSeconds: 31536000,
staleWhileRevalidateSeconds: 900,
}
: false,
});
})
.match('/clear-cache', clearCache)
.match('/service-worker.js', ({ serviceWorker }) => {
serviceWorker('.nuxt/dist/client/service-worker.js')
})
.match('/', homeRedirect)
.match('/api/:path*', apiFallback)
.match('/clear-cache', clearCache)
.match('/sitemap.xml', sitemap)
.match('/feed/:country', feed)
.use(nuxtRoutes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment