Skip to content

Expo Router v3 adds API Routes beta and web bundle splitting

updated

Expo has released Expo Router v3, for Expo SDK 50. The release introduces beta support for a new Expo platform, Servers, which Expo describes as making Expo Router the first universal, full-stack React framework.

API Routes (beta)

API Routes are a zero-config system for creating server endpoints with a unified build process. Adding a +api.js extension to a route makes it render only on the server. Routes can export GET, POST, PUT, PATCH, DELETE, HEAD and OPTIONS functions, and unsupported methods return 405: Method not allowed. In development they are served from the same dev server as the app and website. In production they must be deployed to a dynamic hosting service. API Routes remain in beta throughout v3.

Related additions include:

  • Relative fetch requests on native, enabled by setting the production server URL in app.json.
  • A +not-found.js convention for matching remaining requests after API routes are processed. Projects that use API Routes should rename a top-level catch-all like app/[...missing].js to it.

Expo says the new server architecture will be used to render universal React Server Components in an upcoming release.

Web and performance

  • Route-based bundle splitting on web via Async Routes, with eager chunk loading to avoid network waterfalls. Native support is planned alongside React Server Components.
  • npx expo export -p web is over 2x faster for static sites, dropping from about 23s to about 11s for an average project.
  • The base production web bundle is 30% smaller, going from 1.48mb to 1.05mb. URL and URLSearchParams are now built into the expo package.
  • Fonts loaded with expo-font are automatically extracted and preloaded on web when using static or server output.

Other changes

  • A Jest-based testing library for Expo Router is now public.
  • Link gains target, rel and download props on web, className support, and a push prop.
  • The app directory is now configurable.
  • Other additions:
    • experimental experiments.baseUrl support
    • mjs and cjs support, so .mjs modules can be imported
    • system links like mailto: and sms:
    • improved Typed Routes
    • monorepo support without expo-yarn-workspaces
  • CSS is enabled by default with Metro web. tsconfigPaths is enabled by default in @expo/metro-config.
  • @expo/webpack-config is deprecated in favor of Metro web.
  • Source code and issue tracking moved to the expo/expo monorepo.

Breaking changes

  • expo-router/babel has been removed in favor of babel-preset-expo. Clear the Metro cache with npx expo start --clear.
  • router.push now always pushes routes. The previous behavior is available as router.navigate.
  • react-native-gesture-handler is no longer added automatically. It can be added to use the <Drawer /> navigator.
  • Transpiled code now ships in expo-router/build/* instead of src.
  • SplashScreen should be imported from expo-splash-screen.
  • hrefAttrs on Link should be migrated to top-level props.

Compatible versions include expo@^50.0.0, expo-router@^3.0.0 and react-native@~0.73.2. Snack does not support Expo Router or API routes. Expo will host an SDK 50 launch live-stream on YouTube on January 31st.

Example

import { useFonts } from 'expo-font';

export default function RootLayout() {
  // `loaded` will be `true` in static websites as the font was eagerly loaded with the HTML before this JS was executed.
  const [loaded] = useFonts({
    inter: require('@/fonts/inter.ttf'),
  });

  if (!loaded) {
    // This will no longer be called on static web, meaning the entire boundary will be statically rendered to searchable HTML.
    return null;
  }

  return <Stack />;
}

Source: expo.dev