Building an Offline-First Next.js 15 App with App Router and Dynamic Routes #82498
-
Hello everyone, I'm currently working on a Next.js 15 application using the App Router, and I'm facing a significant challenge in making the entire application work in offline mode, especially with Dynamic Routes. The core idea is this: The Problem: Pre-caching the essential application assets (JS and CSS files). When a user requests an uncached URL while offline, the Service Worker would serve a single fallback HTML page. This fallback page would then load the necessary Client Components from the cache to render the data, perhaps from Local Storage. I've been trying to implement this using Serwist (formerly Workbox) with the Next.js App Router, but I'm running into some difficulties. Specific questions I need help with: Is this the correct and recommended approach for handling dynamic routes in an offline-first Next.js app? How can I properly configure Serwist to work with the Next.js App Router and handle this exact scenario? Are there any practical examples or code snippets that demonstrate this implementation? (Specifically, the configurations for next.config.ts and the Serwist sw.ts file). and I can't find a clear, official guide on how to set up a fallback offline page. The examples I've found online haven't worked for me, and I'm unsure if I'm missing a core concept or if the documentation is simply lacking for this specific use case. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Hi @TarekAbdelfatah, You’re on the right track thinking about SPA-like fallback for offline support with dynamic routes in Next.js 15’s App Router, but this use case is indeed tricky because static pre-caching all dynamic pages is impossible and Next.js’ default static optimizations don’t fully cover this. 1. Conceptual approach: Offline-first with fallback page
2. Service Worker (Serwist/Workbox) configuration
Here’s a sketch for import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching';
import { registerRoute, NavigationRoute } from 'workbox-routing';
// Precache files injected by build process
precacheAndRoute(self.__WB_MANIFEST || []);
// SPA fallback to index.html for navigation requests
const handler = createHandlerBoundToURL('/index.html');
const navigationRoute = new NavigationRoute(handler, {
denylist: [/^\/_next\//, /^\/api\//], // Don't fallback API or static assets requests
});
registerRoute(navigationRoute);
// Optionally add runtime caching for API calls here Make sure 3. Next.js config (
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the replay, I will apply this solution and share my feedback soon |
Beta Was this translation helpful? Give feedback.
-
Hi, In our PWA guide we reference this repository for offline support:
I've found some discussions on that repo though about Offline Mode Support, serwist/serwist#205, and people struggling it to get it to work. One of the suggestions seems to point to using this https://nextjs.org/docs/app/getting-started/linking-and-navigating#native-history-api which is basically shallow rendering for your app, but then useParams doesn't really work - but usePathname and useSearchParams do work. |
Beta Was this translation helpful? Give feedback.
-
Based on this discussion, I found a solution to my case, but it needed some improvement. First, you should make your application a PWA and use Serwist:
My setup is as follows: next.config.ts
sw.ts
I created an offlinedoc page to serve as a fallback when navigating offline to my doc page. Therefore, I repeated the logic of doc in offlinedoc. You can also create a single component and use it in both pages, but make it accept an ID as a parameter: When online, get it from the URL. When offline, extract it from the path. My doc page is located at: my doc page found in src\app\doc\[id]\page.tsx
the offlinedoc code
and my home page is
|
Beta Was this translation helpful? Give feedback.
Based on this discussion, I found a solution to my case, but it needed some improvement.
my code here
https://github.com/TarekAbdelfatah/Pwa-Offline-first-Nextjs-dynamicRoutes.git
First, you should make your application a PWA and use Serwist:
My setup is as follows:
next.config.ts