spa-loading-template.html
When a page is rendered with ssr: false (either globally in nuxt.config or for specific routes with routeRules), the server returns an HTML shell without server-rendered application content. Your page content is not visible until the JavaScript bundle has been downloaded, Nuxt plugins have run, and the first page has rendered.
You can fill that gap by adding an app/spa-loading-template.html file. Its contents are inserted directly into the HTML returned by the server, so they are visible before the Nuxt client bundle has loaded.
<!-- https://github.com/barelyhuman/snips/blob/dev/pages/css-loader.md -->
<div class="loader"></div>
<style>
.loader {
position: fixed;
top: 50%;
left: 50%;
width: 24px;
height: 24px;
margin: -12px 0 0 -12px;
border: 2px solid #efefef;
border-top-color: #000;
border-radius: 50%;
animation: loader 600ms linear infinite;
}
@keyframes loader {
to { transform: rotate(360deg); }
}
</style><NuxtLoadingIndicator> component.When It Is Removed
By default, the loading template is rendered next to the Nuxt app root rather than inside it:
<div id="__nuxt"></div>
<div id="__nuxt-loader"><!-- spa-loading-template.html --></div>加载模板会一直显示,直到首个页面(包括页面中的异步 setup)准备渲染,随后被移除。这可以避免加载画面与页面之间出现白屏。你可以使用 app.spaLoaderTag 和 app.spaLoaderAttrs 自定义包装元素。Nuxt 会通过 id 移除该元素,因此修改 spaLoaderAttrs 时请保留 id。
You can render it inside the app root instead by setting the experimental spaLoadingTemplateLocation option to 'within'. In that case, it is removed as soon as Vue mounts the app, so the screen may be blank until the first page is ready to render.
Configuration
Nuxt looks for spa-loading-template.html in the app/ directory (more precisely, the dir.app directory) of your project and of each layer.
You can change this with the spaLoadingTemplate option:
- a path (relative to
srcDir, which isapp/by default) to use a different HTML file, trueto use Nuxt's built-in loading screen when no file is found,falseto disable the loading template entirely.
export default defineNuxtConfig({
ssr: false,
spaLoadingTemplate: 'my-loader.html',
})