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 43const { getDefaultConfig } = require('expo/metro-config'); 44 45/** @type {import('expo/metro-config').MetroConfig} */ 46const config = getDefaultConfig(__dirname, { 47 // Additional features... 48}); 49 50module.exports = config; 51``` 52 53 You can also [learn more](/guides/customizing-metro/) about customizing Metro. 54 55</Step> 56 57<Step label="3"> 58 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. 59 60 <Tabs> 61 <Tab label="Yarn"> 62 ```json package.json 63 { 64 /* @hide ... */ 65 /* @end */ 66 "resolutions": { 67 "react-refresh": "~0.14.0" 68 } 69 } 70 ``` 71 </Tab> 72 <Tab label="npm"> 73 ```json package.json 74 { 75 /* @hide ... */ 76 /* @end */ 77 "overrides": { 78 "react-refresh": "~0.14.0" 79 } 80 } 81 ``` 82 </Tab> 83 </Tabs> 84</Step> 85 86<Step label="4"> 87 Finally, start the development server: 88 89 <Terminal cmd={['$ npx expo start']} /> 90</Step> 91 92## Production 93 94To bundle your static website for production, run the universal export command: 95 96<Terminal cmd={['$ npx expo export --platform web']} /> 97 98This 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. 99You can test the production build locally by running the following command and opening the linked URL in your browser: 100 101<Terminal cmd={['$ npx serve dist']} /> 102 103This 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. 104 105## Dynamic Routes 106 107The `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. 108 109```js app/blog/[id].tsx 110import { Text } from 'react-native'; 111import { useLocalSearchParams } from 'expo-router'; 112 113/* @info This method is run in a Node.js environment at build-time. */ 114export async function generateStaticParams(): Promise<Record<string, string>[]> { 115 /* @end */ 116 const posts = await getPosts(); 117 // Return an array of params to generate static HTML files for. 118 // Each entry in the array will be a new page. 119 return posts.map(post => ({ id: post.id })); 120} 121 122export default function Page() { 123 const { id } = useLocalSearchParams(); 124 125 return <Text>Post {id}</Text>; 126} 127``` 128 129This 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: 130 131<FileTree files={['dist/blog/alpha.html', 'dist/blog/beta.html']} /> 132 133<APIBox header="generateStaticParams"> 134 135A 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**. 136 137```js app/[id].tsx 138export async function generateStaticParams(): Promise<Record<string, string>[]> { 139 /* @info Prints the current working directory */ 140 console.log(process.cwd()); 141 /* @end */ 142 143 return []; 144} 145``` 146 147**generateStaticParams** cascades from nested parents down to children. The cascading parameters are passed to every dynamic child route that exports **generateStaticParams**. 148 149```js app/[id]/_layout.tsx 150export async function generateStaticParams(): Promise<Record<string, string>[]> { 151 /* @info Any dynamic children that export <b>generateStaticParams</b> will be invoked once for every entry in the array. */ 152 return [{ id: 'one' }, { id: 'two' }]; 153 /* @end */ 154} 155``` 156 157Now the dynamic child routes will be invoked twice, once with `{ id: 'one' }` and once with `{ id: 'two' }`. All variations must be accounted for. 158 159```js app/[id]/[comment].tsx 160export async function generateStaticParams(params: { 161 id: 'one' | 'two', 162}): Promise<Record<string, string>[]> { 163 const comments = await getComments(params.id); 164 return comments.map(comment => ({ 165 /* @info Ensure the parent properties are passed down too. */ 166 ...params, 167 /* @end */ 168 comment: comment.id, 169 })); 170} 171``` 172 173</APIBox> 174 175## Root HTML 176 177By default, every page is wrapped with some small HTML boilerplate, this is known as the **root HTML**. 178 179You 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. 180 181> **Note**: Global context providers should go in the [Root Layout](/router/advanced/root-layout) component, not the Root HTML component. 182 183```js app/+html.tsx 184import { ScrollViewStyleReset } from 'expo-router/html'; 185 186// This file is web-only and used to configure the root HTML for every 187// web page during static rendering. 188// The contents of this function only run in Node.js environments and 189// do not have access to the DOM or browser APIs. 190export default function Root({ children }: { children: React.ReactNode }) { 191 return ( 192 <html lang="en"> 193 <head> 194 <meta charSet="utf-8" /> 195 <meta httpEquiv="X-UA-Compatible" content="IE=edge" /> 196 197 {/* 198 This viewport disables scaling which makes the mobile website act more like a native app. 199 However this does reduce built-in accessibility. If you want to enable scaling, use this instead: 200 <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" /> 201 */} 202 <meta 203 name="viewport" 204 content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1.00001,viewport-fit=cover" 205 /> 206 {/* 207 Disable body scrolling on web. This makes ScrollView components work closer to how they do on native. 208 However, body scrolling is often nice to have for mobile web. If you want to enable it, remove this line. 209 */} 210 <ScrollViewStyleReset /> 211 212 {/* Add any additional <head> elements that you want globally available on web... */} 213 </head> 214 <body>{children}</body> 215 </html> 216 ); 217} 218``` 219 220- The `children` prop comes with the root `<div id="root" />` tag included inside. 221- The JavaScript scripts are appended after the static render. 222- React Native web styles are statically injected automatically. 223- Global CSS should not be imported into this file. Instead, use the [Root Layout](/router/advanced/root-layout) component. 224- Browser APIs like `window.location` are unavailable in this component as it only runs in Node.js during static rendering. 225 226### `expo-router/html` 227 228The exports from `expo-router/html` are related to the Root HTML component. 229 230- `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. 231 232## Meta Tags 233 234You can add meta tags to your pages with the [`<Head />`](/docs/features/head) module from `expo-router`: 235 236```js app/about.tsx 237import Head from 'expo-router/head'; 238import { Text } from 'react-native'; 239 240export default function Page() { 241 return ( 242 <> 243 <Head> 244 <title>My Blog Website</title> 245 <meta name="description" content="This is my blog." /> 246 </Head> 247 <Text>About my blog</Text> 248 </> 249 ); 250} 251``` 252 253The 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. 254 255## Static Files 256 257Expo 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. 258 259<FileTree 260 files={['public/favicon.ico', 'public/logo.png', 'public/.well-known/apple-app-site-association']} 261/> 262 263These files will be copied to the **dist** folder during static rendering: 264 265<FileTree 266 files={[ 267 'dist/index.html', 268 'dist/favicon.ico', 269 'dist/logo.png', 270 'dist/.well-known/apple-app-site-association', 271 'dist/_expo/static/js/index-xxx.js', 272 'dist/_expo/static/css/index-xxx.css', 273 ]} 274/> 275 276> **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`: 277 278```js app/index.tsx 279import { Image } from 'react-native'; 280 281export default function Page() { 282 return <Image source={{ uri: '/logo.png' }} />; 283} 284``` 285 286## FAQ 287 288### How do I add a custom server? 289 290As 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. 291 292In 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. 293 294## Server-side Rendering 295 296Rendering at request-time (SSR) is not supported in `web.output: 'static'`. This will likely be added in a future version of Expo Router. 297 298### Where can I deploy statically rendered websites? 299 300You can deploy your statically rendered website to any static hosting service. Here are some popular options: 301 302- [Netlify](https://www.netlify.com/) 303- [Cloudflare Pages](https://pages.cloudflare.com/) 304- [AWS Amplify](https://aws.amazon.com/amplify/) 305- [Vercel](https://vercel.com/) 306- [GitHub Pages](https://pages.github.com/) 307- [Render](https://render.com/) 308- [Surge](https://surge.sh/) 309 310> 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. 311