Skip to content

Expo Router v2 adds typed routes, static rendering and CSS

updated

Expo Router v2 has been released, following five months of Expo Router v1. A new project can be created with one command:

npx create-expo-app@latest --template tabs@49

Automatic TypeScript

Expo CLI can now continuously generate static type definitions for every route in an Expo Router app (Typed Routes). Expo CLI also has first-class support for tsconfig.json paths and baseUrl, so babel-plugin-module-resolver can be removed from babel.config.js if it was used for this. A generated typed environment file adds React Native Web properties to React Native types, reducing ts-expect-error annotations.

Static routes

Expo Router v2 is the first version of Expo web with first-class support for build-time static generation and the Node.js environment. All routes can be rendered to static HTML, while the Link component continues to use client-side navigation. A new expo-router/head component defines static or dynamic head metadata, and a new app/+html.js file configures the default HTML wrapper for each route. Static output is enabled in app.json with "bundler": "metro" and "output": "static" under web. Most of the Expo SDK has been updated to run in Node.js environments.

CSS and styling

Expo CLI now supports global CSS and CSS inside node modules, Sass and SCSS, CSS Modules, PostCSS configuration files and plugins, and Tailwind CSS. These are shimmed out on native. CSS is enabled through getDefaultConfig in metro.config.js with isCSSEnabled: true, and CSS Modules use the module.css extension.

Async routes

Developed with the React Developer Experience team at Meta, async routes add lazy bundling so an app can be bundled one page at a time, and errors are deferred until a page loads. The feature is experimental, only enabled in development, and currently doesn't support static rendering.

Universal linking

Expo Router creates universal links to every route automatically, and v2 further automates associating an app with a website. Expo Head now has iOS support for features such as handoff between web and native, Apple notes on any page, and contextualized Siri commands.

Additional features

  • Support for a src/app directory
  • A new imperative router export and router.canGoBack()
  • web.favicon in app.json generates favicons at build time
  • The root index.js is no longer required; point main to expo-router/entry
  • Support for Reanimated Shared Element Transitions and expo-dev-client
  • A default scheme is no longer required
  • Expo Webpack has moved to maintenance mode; Expo Router is now the preferred way to build websites with Expo

Migration

Expo Router v2 is designed for Expo SDK 49. After upgrading, run npx expo install --fix, re-build any dev client, replace useSearchParams with useGlobalSearchParams or useLocalSearchParams, use <Head /> for web document titles, and migrate navigation.canGoBack to router.canGoBack.

Source: evanbacon.dev