It enables you to set different configurations for different groups of pages:
pages/(marketing)/+Layout.js # The layout for all marketing pagespages/(marketing)/index/+Page.jspages/(marketing)/about/+Page.jspages/admin-panel/+Layout.js # The layout for all admin pagespages/admin-panel/index/+Page.jspages/admin-panel/users/+Page.js
You can use Route Functions to get full programmatic flexibility for advanced routing logic.
// /pages/product/edit/+route.js// Environment: server & client// This file defines the route of /pages/product/edit/+Page.js// We use a RegExp, but we could as well use a routing library.import partRegex from 'part-regex'const routeRegex = partRegex`/product/${/([0-9]+)/}/edit`// Route Functionexport function route(pageContext) { const match = pageContext.urlPathname.match(routeRegex) if (!match) return false const [, id] = match return { routeParams: { id } }}
// /pages/product/edit/+route.ts// Environment: server & client// This file defines the route of /pages/product/edit/+Page.tsimport type { PageContext } from 'vike/types'// We use a RegExp, but we could as well use a routing library.import partRegex from 'part-regex'const routeRegex = partRegex`/product/${/([0-9]+)/}/edit`// Route Functionexport function route(pageContext: PageContext) { const match = pageContext.urlPathname.match(routeRegex) if (!match) return false const [, id] = match return { routeParams: { id } }}
You can use a guard() hook to protect pages from unauthorized/unexpected access.
// /pages/admin/+guard.js// Environment: serverimport { render } from 'vike/abort'// This guard() hook protects all pages /pages/admin/**/+Page.jsexport async function guard(pageContext) { if (!pageContext.user.isAdmin) { throw render(401, "You aren't allowed to access this page.") }}
// /pages/admin/+guard.ts// Environment: serverimport type { PageContextServer } from 'vike/types'import { render } from 'vike/abort'// This guard() hook protects all pages /pages/admin/**/+Page.tsexport async function guard(pageContext: PageContextServer) { if (!pageContext.user.isAdmin) { throw render(401, "You aren't allowed to access this page.") }}