defineRouteRules

在页面级别定义混合渲染的路由规则。
该功能处于实验阶段,使用前需在 nuxt.config 中启用 experimental.inlineRouteRules 选项。

用法

pages/index.vue
<script setup lang="ts">
defineRouteRules({
  prerender: true
})
</script>

<template>
  <h1>Hello world!</h1>
</template>

将会被转换为:

nuxt.config.ts
export default defineNuxtConfig({
  routeRules: {
    '/': { prerender: true }
  }
})
运行 nuxt build 时,主页会被预渲染到 .output/public/index.html 并作为静态资源进行服务。

说明

  • ~/pages/foo/bar.vue 中定义的规则将应用于 /foo/bar 请求。
  • ~/pages/foo/[id].vue 中定义的规则将应用于 /foo/** 请求。

如果需要更细粒度的控制,比如在页面的 definePageMeta 中使用了自定义的 pathalias,应当直接在 nuxt.config 中设置 routeRules

阅读更多关于 routeRules 的内容。