Expo Router v3 adds API Routes beta and web bundle splitting
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
fetchrequests on native, enabled by setting the production server URL inapp.json. - A
+not-found.jsconvention for matching remaining requests after API routes are processed. Projects that use API Routes should rename a top-level catch-all likeapp/[...missing].jsto 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 webis 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.
URLandURLSearchParamsare now built into theexpopackage. - Fonts loaded with
expo-fontare 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.
Linkgainstarget,relanddownloadprops on web,classNamesupport, and apushprop.- The app directory is now configurable.
- Other additions:
- experimental
experiments.baseUrlsupport - mjs and cjs support, so
.mjsmodules can be imported - system links like
mailto:andsms: - improved Typed Routes
- monorepo support without
expo-yarn-workspaces
- experimental
- CSS is enabled by default with Metro web.
tsconfigPathsis enabled by default in@expo/metro-config. @expo/webpack-configis deprecated in favor of Metro web.- Source code and issue tracking moved to the expo/expo monorepo.
Breaking changes
expo-router/babelhas been removed in favor ofbabel-preset-expo. Clear the Metro cache withnpx expo start --clear.router.pushnow always pushes routes. The previous behavior is available asrouter.navigate.react-native-gesture-handleris no longer added automatically. It can be added to use the<Drawer />navigator.- Transpiled code now ships in
expo-router/build/*instead ofsrc. SplashScreenshould be imported fromexpo-splash-screen.hrefAttrsonLinkshould 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 />;
}
Links
Source: expo.dev