Building Static Sites
Analog supports Static Site Generation when building for deployment. This includes prerendering provided routes to static HTML files along with the client-side application.
Static Site Generation
To prerender pages, use the prerender
property to configure routes to be rendered at build time. The routes to be prerendered can be provided asynchronously also.
import { defineConfig } from 'vite';
import analog from '@analogjs/platform';
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
plugins: [
analog({
prerender: {
routes: async () => [
'/',
'/about',
'/blog',
'/blog/posts/2023-02-01-my-first-post',
],
},
}),
],
}));
To only prerender the static pages, use the static: true
flag.
import { defineConfig } from 'vite';
import analog from '@analogjs/platform';
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
plugins: [
analog({
static: true,
prerender: {
routes: async () => [
'/',
'/about',
'/blog',
'/blog/posts/2023-02-01-my-first-post',
],
},
}),
],
}));
The static pages can be deployed from the dist/analog/public
directory.
Sitemap Generation
Analog also supports automatic sitemap generation. Analog generates a sitemap in the dist/analog/public
directory when running a build if a sitemap configuration is provided.
import { defineConfig } from 'vite';
import analog from '@analogjs/platform';
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
plugins: [
analog({
prerender: {
routes: async () => ['/', '/blog'],
sitemap: {
host: 'https://analogjs.org/',
},
},
}),
],
}));
As long as routes are provided, Analog generates a sitemap.xml
file containing a
mapping of the pages' <loc>
and <lastmod>
properties.
<?xml version="1.0" encoding="UTF-8"?>
<urlset...>
<!--This file was automatically generated by Analog.-->
<url>
<loc>https://analogjs.org/</loc>
<lastmod>2023-07-01</lastmod>
</url>
<url>
<loc>https://analogjs.org/blog</loc>
<lastmod>2023-07-01</lastmod>
</url>
</urlset...>