1--- 2title: Static Rendering 3description: Learn how to render routes to static HTML and CSS files with Expo Router. 4hideFromSearch: true 5--- 6 7import { Terminal } from '~/ui/components/Snippet'; 8import { FileTree } from '~/ui/components/FileTree'; 9import { Step } from '~/ui/components/Step'; 10import { Tabs, Tab } from '~/ui/components/Tabs'; 11import { APIBox } from '~/components/plugins/APIBox'; 12 13> Available from Expo SDK 49 and Expo Router v2. 14 15To enable Search Engine Optimization (SEO) on the web you must statically render your app. This guide will walk you through the process of statically rendering your Expo Router app. 16 17## Setup 18 19<Step label="1"> 20 Enable metro bundler and static rendering in the project's [app config](/versions/latest/config/app/): 21 22```json app.json 23{ 24 "expo": { 25 /* @hide ... */ 26 /* @end */ 27 "web": { 28 /* @info Static rendering is only supported with Metro bundler and Expo Router */ 29 "bundler": "metro", 30 /* @end */ 31 "output": "static" 32 } 33 } 34} 35``` 36 37</Step> 38 39<Step label="2"> 40 If you have a **metro.config.js** file in your project, ensure it extends **expo/metro-config** as shown below: 41 42```js metro.config.js 43// Learn more: https://docs.expo.dev/guides/customizing-metro/ 44const { getDefaultConfig } = require('expo/metro-config'); 45 46/** @type {import('expo/metro-config').MetroConfig} */ 47const config = getDefaultConfig(__dirname, { 48 // Additional features... 49}); 50 51module.exports = config; 52``` 53 54</Step> 55 56<Step label="3"> 57 Expo Router requires at least `[email protected]`. React Native hasn't upgraded yet, so you need to force upgrade your `react-refresh` version by setting a Yarn resolution or npm override. 58 59 <Tabs> 60 <Tab label="Yarn"> 61 ```json package.json 62 { 63 /* @hide ... */ 64 /* @end */ 65 "resolutions": { 66 "react-refresh": "~0.14.0" 67 } 68 } 69 ``` 70 </Tab> 71 <Tab label="npm"> 72 ```json package.json 73 { 74 /* @hide ... */ 75 /* @end */ 76 "overrides": { 77 "react-refresh": "~0.14.0" 78 } 79 } 80 ``` 81 </Tab> 82 </Tabs> 83</Step> 84 85<Step label="4"> 86 Finally, start the development server: 87 88 <Terminal cmd={['$ npx expo start']} /> 89</Step> 90 91## Production 92 93To bundle your static website for production, run the universal export command: 94 95<Terminal cmd={['$ npx expo export --platform web']} /> 96 97This will create a **dist** directory with your statically rendered website. If you have files in a local **public** directory, these will be copied over as well. 98You can test the production build locally by running the following command and opening the linked URL in your browser: 99 100<Terminal cmd={['$ npx serve dist']} /> 101 102This project can be deployed to almost every hosting service. Note that this is not a single-page application, nor does it contain a custom server API. This means dynamic routes (**app/[id].tsx**) will not arbitrarily work. You may need to build a serverless function to handle dynamic routes. 103 104## Dynamic Routes 105 106The `static` output will generate HTML files for each route. This means dynamic routes (**app/[id].tsx**) will not work out of the box. You can generate known routes ahead of time using the `generateStaticParams` function. 107 108```js app/blog/[id].tsx 109import { Text } from 'react-native'; 110import { useLocalSearchParams } from 'expo-router'; 111 112/* @info This method is run in a Node.js environment at build-time. */ 113export async function generateStaticParams(): Promise<Record<string, string>[]> { 114 /* @end */ 115 const posts = await getPosts(); 116 // Return an array of params to generate static HTML files for. 117 // Each entry in the array will be a new page. 118 return posts.map(post => ({ id: post.id })); 119} 120 121export default function Page() { 122 const { id } = useLocalSearchParams(); 123 124 return <Text>Post {id}</Text>; 125} 126``` 127 128This will output a file for each post in the **dist** directory. For example, if the `generateStaticParams` method returned `[{ id: "alpha" }, { id: "beta" }]`, the following files would be generated: 129 130<FileTree files={['dist/blog/alpha.html', 'dist/blog/beta.html']} /> 131 132<APIBox header="generateStaticParams"> 133 134A server-only function evaluated at build-time in a Node.js environment by Expo CLI. This means it has access to `__dirname`, `process.cwd()`, `process.env`, and more. It also has access to every environment variable that's available in the process, not just the values prefixed with **EXPO*PUBLIC***. **generateStaticParams** is not run in a browser environment, so it cannot access browser APIs like **localStorage** or **document**, nor can it access native Expo APIs such as **expo-camera** or **expo-location**. 135 136```js app/[id].tsx 137export async function generateStaticParams(): Promise<Record<string, string>[]> { 138 /* @info Prints the current working directory */ 139 console.log(process.cwd()); 140 /* @end */ 141 142 return []; 143} 144``` 145 146**generateStaticParams** cascades from nested parents down to children. The cascading parameters are passed to every dynamic child route that exports **generateStaticParams**. 147 148```js app/[id]/_layout.tsx 149export async function generateStaticParams(): Promise<Record<string, string>[]> { 150 /* @info Any dynamic children that export <b>generateStaticParams</b> will be invoked once for every entry in the array. */ 151 return [{ id: 'one' }, { id: 'two' }]; 152 /* @end */ 153} 154``` 155 156Now the dynamic child routes will be invoked twice, once with `{ id: 'one' }` and once with `{ id: 'two' }`. All variations must be accounted for. 157 158```js app/[id]/[comment].tsx 159export async function generateStaticParams(params: { 160 id: 'one' | 'two', 161}): Promise<Record<string, string>[]> { 162 const comments = await getComments(params.id); 163 return comments.map(comment => ({ 164 /* @info Ensure the parent properties are passed down too. */ 165 ...params, 166 /* @end */ 167 comment: comment.id, 168 })); 169} 170``` 171 172</APIBox> 173 174## Root HTML 175 176By default, every page is wrapped with some small HTML boilerplate, this is known as the **root HTML**. 177 178You can customize the root HTML file by creating an **app/+html.tsx** file in your project. This file exports a React component that only ever runs in Node.js, which means global CSS cannot be imported inside of it. The component will wrap all routes in the **app** directory. This is useful for adding global `<head>` elements or disabling body scrolling. 179 180> **Note**: Global context providers should go in the [Root Layout](/router/advanced/root-layout) component, not the Root HTML component. 181 182```js app/+html.tsx 183import { ScrollViewStyleReset } from 'expo-router/html'; 184 185// This file is web-only and used to configure the root HTML for every 186// web page during static rendering. 187// The contents of this function only run in Node.js environments and 188// do not have access to the DOM or browser APIs. 189export default function Root({ children }: { children: React.ReactNode }) { 190 return ( 191 <html lang="en"> 192 <head> 193 <meta charSet="utf-8" /> 194 <meta httpEquiv="X-UA-Compatible" content="IE=edge" /> 195 196 {/* 197 This viewport disables scaling which makes the mobile website act more like a native app. 198 However this does reduce built-in accessibility. If you want to enable scaling, use this instead: 199 <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" /> 200 */} 201 <meta 202 name="viewport" 203 content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1.00001,viewport-fit=cover" 204 /> 205 {/* 206 Disable body scrolling on web. This makes ScrollView components work closer to how they do on native. 207 However, body scrolling is often nice to have for mobile web. If you want to enable it, remove this line. 208 */} 209 <ScrollViewStyleReset /> 210 211 {/* Add any additional <head> elements that you want globally available on web... */} 212 </head> 213 <body>{children}</body> 214 </html> 215 ); 216} 217``` 218 219- The `children` prop comes with the root `<div id="root" />` tag included inside. 220- The JavaScript scripts are appended after the static render. 221- React Native web styles are statically injected automatically. 222- Global CSS should not be imported into this file. Instead, use the [Root Layout](/router/advanced/root-layout) component. 223- Browser APIs like `window.location` are unavailable in this component as it only runs in Node.js during static rendering. 224 225### `expo-router/html` 226 227The exports from `expo-router/html` are related to the Root HTML component. 228 229- `ScrollViewStyleReset`: Root style-reset for full-screen [React Native web apps](https://necolas.github.io/react-native-web/docs/setup/#root-element) with a root `<ScrollView />` should use the following styles to ensure native parity. 230 231## Meta Tags 232 233You can add meta tags to your pages with the [`<Head />`](/docs/features/head) module from `expo-router`: 234 235```js app/about.tsx 236import Head from 'expo-router/head'; 237import { Text } from 'react-native'; 238 239export default function Page() { 240 return ( 241 <> 242 <Head> 243 <title>My Blog Website</title> 244 <meta name="description" content="This is my blog." /> 245 </Head> 246 <Text>About my blog</Text> 247 </> 248 ); 249} 250``` 251 252The head elements can be updated dynamically using the same API. However, it's useful for SEO to have static head elements rendered ahead of time. 253 254## Static Files 255 256Expo CLI supports a root **public** directory that gets copied to the **dist** folder during static rendering. This is useful for adding static files like images, fonts, and other assets. 257 258<FileTree 259 files={['public/favicon.ico', 'public/logo.png', 'public/.well-known/apple-app-site-association']} 260/> 261 262These files will be copied to the **dist** folder during static rendering: 263 264<FileTree 265 files={[ 266 'dist/index.html', 267 'dist/favicon.ico', 268 'dist/logo.png', 269 'dist/.well-known/apple-app-site-association', 270 'dist/_expo/static/js/index-xxx.js', 271 'dist/_expo/static/css/index-xxx.css', 272 ]} 273/> 274 275> **info** **Web only**: Static assets can be accessed in runtime code using relative paths. For example, the **logo.png** can be accessed at `/logo.png`: 276 277```js app/index.tsx 278import { Image } from 'react-native'; 279 280export default function Page() { 281 return <Image source={{ uri: '/logo.png' }} />; 282} 283``` 284 285## FAQ 286 287### How do I add a custom server? 288 289As of Expo Router v2 there is no prescriptive way to add a custom server. You can use any server you want. However, you will need to handle dynamic routes yourself. You can use the `generateStaticParams` function to generate static HTML files for known routes. 290 291In future, there will be a server API, and a new `web.output` mode which will generate a project that will (amongst other things) support dynamic routes. 292 293## Server-side Rendering 294 295Rendering at request-time (SSR) is not supported in `web.output: 'static'`. This will likely be added in a future version of Expo Router. 296 297### Where can I deploy statically rendered websites? 298 299You can deploy your statically rendered website to any static hosting service. Here are some popular options: 300 301- [Netlify](https://www.netlify.com/) 302- [Cloudflare Pages](https://pages.cloudflare.com/) 303- [AWS Amplify](https://aws.amazon.com/amplify/) 304- [Vercel](https://vercel.com/) 305- [GitHub Pages](https://pages.github.com/) 306- [Render](https://render.com/) 307- [Surge](https://surge.sh/) 308 309> Note: You don't need to add Single-Page Application styled redirects to your static hosting service. The static website is not a single-page application. It is a collection of static HTML files. 310