1. Analyze the Request:
* Topic: Static Export Fallback Plan.
* Key Components:
* CDN Routing (how to handle failures or redirects at the edge).
* Stale Snapshots (serving old content if new content fails or is missing).
* Error Overlays (UI handling for when things go wrong).
* Format: A "plan" – structured, actionable, and technical.
-
Deconstruct the Components & Brainstorm Strategies:
-
Context: "Static Export" usually implies a JAMstack, Next.js output: export, Gatsby, or Hugo site served directly from storage (S3) or a CDN, without a running Node server for SSR (Server-Side Rendering). This limits dynamic error handling, so edge logic and client-side logic are crucial.
-
Component 1: CDN Routing (The Edge Layer)
- Goal: If the requested path doesn't exist (404), or if the origin is down, what happens?
- Strategy: Rewrite rules.
- Scenario: A user requests
/blog/my-post. The file is /blog/my-post.html.
- Failure: The file isn't there.
- Fallback:
- Serve a custom 404.html (standard).
- Serve a "maintenance" page if origin is unhealthy.
- Redirect specific legacy paths to new ones.
- Advanced: "Shadowing" – if
/A is missing, try serving /B before erroring.
-
Component 2: Stale Snapshots (Data/Content Resilience)
- Goal: If the build process fails or the data fetch (at build time) errors, don't break the site. Serve the last known good version.
- Strategy:
- CI/CD: Atomic deploys. Don't delete the old version until the new one is ready. If build fails, don't promote.
- CDN Cache: Stale-While-Revalidate or Stale-If-Error headers.
- Client-Side: LocalStorage/IndexedDB caching of API responses (less relevant for pure static export, but relevant for hydration). Let's focus on the build/deploy aspect first.
- Rollback: Ability to instantly switch the CDN pointer back to the previous artifact.
-
Component 3: Error Overlays (The User Interface)
- Goal: If a specific chunk fails to load (e.g., network drop) or a client-side crash occurs, show a nice message, not a white screen of death.
- Strategy:
- Global Boundary: React
ErrorBoundary.
- Chunk Load Failures: Webpack/Vite dynamic import error handling.
- Styling: Needs to be inline (in the head) so it loads even if CSS chunks fail.
-
Structure the Plan:
- Objective: Brief summary.
- Phase 1: Infrastructure & CDN (The First Line of Defense). Focus on routing and availability.
- Phase 2: Content & Availability (Stale Data). Focus on atomic deploys and rollback.
- Phase 3: Client-Side Experience (Error Overlays). Focus on UI handling.
- Operational Checklist: How to maintain this.
-
Drafting the Content (Iterative Refinement):
-
CDN Routing:
- Need to mention specific providers (Cloudflare, AWS CloudFront) as examples.
- Key concept: Fallback to
index.html for SPA vs. 404.html for SSG. Since the prompt specifies "Static Export," it's likely SSG or Hybrid.
- Action: Configure "Stale-If-Error" on the CDN level.
-
Stale Snapshots:
- This is tricky with static exports because the files are usually immutable.
- Interpretation 1: Incremental Static Regeneration (ISR) fallbacks (Next.js specific). Since the prompt says "Static Export" (often implying
next export which disables ISR), I should focus on Build-Time resilience and Versioning.
- Action: Versioned Buckets (S3).
v1, v2. If v3 fails to build, the CDN keeps pointing to v2.
-
Error Overlays:
- Needs to handle "ChunkLoadError" (common in static sites when a new deploy invalidates old chunks while a user is on the page).
- Solution: Automatic reload prompt.
-
Refining the Tone:
- Professional, architectural, directive.
-
Writing the specific sections (Self-Correction during drafting):
- Correction: Make sure to distinguish between routing failures (page missing) and origin failures (server down). Since it's static, the "server" is S3/Blob storage.
- Detail: Add specific headers like
Surrogate-Key or Cache-Control.
-
Final Polish:
- Organize into a clear hierarchy.
- Add a "Decision Matrix" or "Flowchart" description to make it actionable.
- Ensure code snippets (config examples) are included.
(Self-Correction Example during drafting Phase 2): I initially thought about client-side caching. But "Stale Snapshots" usually refers to the server-side state. If the build fails, we serve the old build. I will focus the "Stale Snapshots" section on Atomic Deploys & Rollback Strategy.
-
Final Output Generation (This matches the provided good response).
- Title: Static Export Fallback & Resilience Plan.
- Sections: Edge/CDN, Deployment/Storage, Client/UI.
-
(Post-generation check): Did I cover CDN routing? Yes. Stale snapshots? Yes. Error overlays? Yes.
Let's write it out.
Static Export Fallback & Resilience Plan
Context: This plan addresses failure modes for statically exported applications (e.g., Next.js output: export, Gatsby, Hugo) hosted on object storage (S3) and delivered via a CDN.
Since static exports lack a running server to handle dynamic logic, resilience must be implemented at the Edge (CDN) layer and the Client (Browser) layer.
1. CDN Routing & Edge Logic
Objective: Ensure availability and correct routing even when files are missing or the origin is unresponsive.
A. The "Shadow" 404 Handling (SPAs)
Single Page Applications (SPAs) rely on client-side routing. If a user refreshes /dashboard, S3 looks for dashboard.html. If missing, it returns 404 (XML).
- Strategy: Configure the CDN to intercept
404 responses from the origin.
- Logic:
- Request:
GET /dashboard
- Origin Response:
404 Not Found
- Edge Override: Ignore the origin's 404 status. Serve
/index.html with a 200 OK status instead.
- Client: The SPA boots up, the router recognizes
/dashboard, and renders the view.
- Note: This must be scoped to exclude actual missing assets (like images). If the request is for
*.jpg, *.css, return the actual 404.
B. Stale-If-Error (Availability)
If the Origin Storage (e.g., S3) goes down, the site should remain online.
2. Stale Snapshots & Rollback Strategy
Objective: Prevent broken deployments ("White Screen of Death") caused by a bad build or broken links.
A. Immutable Versioning (The "Atomic" Release)
Never overwrite index.html or /static/js/main.js in place.
- Structure: Use content hashes in filenames (e.g.,
main.a1b2c3.js).
- Upload: Push new assets to the bucket before updating the entry point.
- Activate: Update the
index.html pointer only after all chunks are verified present.
B. The "Golden Snapshot" Fallback
If a new deployment breaks critical paths, the CDN should fall back to the previous known good version.
- Implementation (Cloudflare Workers / Lambda@Edge):
- Maintain a KV (Key-Value) pair of
CURRENT_VERSION and PREVIOUS_VERSION.
- If a request for a specific JS chunk returns a
404 or 5xx (indicating a broken build reference), the Edge Worker rewrites the request to fetch the corresponding chunk from PREVIOUS_VERSION.
C. "Stale" UI for Client-Side Data
For static exports that fetch dynamic data (API calls) at runtime:
- Problem: The API is down.
- Fallback: LocalStorage / IndexedDB.
- Logic:
- App attempts to fetch data.
- Network fails.
- App checks LocalStorage for the last successful payload for this endpoint.
- If found, render that data with a toast notification: "Offline mode: showing content from [Time]."
3. Error Overlays & Client-Side UX
Objective: Prevent "White Screens" and handle client-side routing failures gracefully.
A. The Error Boundary (React/Vue/Angular)
JavaScript errors should not crash the entire app.
- Component:
<GlobalErrorBoundary>
- Behavior:
- Catch the error.
- Log to monitoring (Sentry/Datadog).
- Render a Static Fallback UI instead of the crashed component tree.
- UI Copy: "Something went wrong. [Try Again] [Go Home]."
B. Chunk Load Failure Handling
When a user stays on a site during a deployment, old code tries to fetch old chunks that no longer exist.
C. Visual Error Overlay Design
Design a specific, on-brand error overlay that is included in the main bundle.
Requirements:
- CSS Inlining: The styles for the error overlay must be inlined in the
<head> to prevent the overlay itself from breaking if CSS chunks fail to load.
- Opacity:
- Critical Failure (500/Network): Full-screen takeover with "System Unavailable."
- Minor Failure (API): Toast or Banner at top of screen. "Data currently unavailable."
Summary Checklist
| Layer | Mechanism | Action |
|---|
| CDN | Stale-If-Error | Serve cached content if S3 is down. |
| CDN | 404 Rewrite | Serve index.html for route paths to support SPA refresh. |
| Deploy | Atomic Upload | Upload assets first, index.html last. |
| Runtime | Chunk Retry | Hard reload the page if JS chunks fail to load. |
| UI | Error Boundary | Render a generic "Oops" page instead of a white screen. |