Static app hosted on S3 while using dynamic routes. Possible? #17711
Replies: 7 comments 14 replies
-
@timneutkens I believe Vercel dashboard has been created in a similar fashion right? I can't get my head around it 🤯 Your help on the subject would be awesome 🙏 |
Beta Was this translation helpful? Give feedback.
-
my understanding of S3 on AWS is that it only hosts static HTML sites; it can not run node.js in the background. I think AWS offers other solutions that can run node, but not S3. |
Beta Was this translation helpful? Give feedback.
-
Edit Just noticed that this is 24 days old. Ah well. I'll leave my reply for the next user :) My own site runs on S3 (with cloudfront) and it works :}. https://bettershifting.com/frequently-asked-questions/ I have an My
I don't have one in |
Beta Was this translation helpful? Give feedback.
-
There's a tricky solution to this problem. Next.js expects you to specify all possible routes during static site generation. However, if you have more number of routes that cannot be defined, it will redirect you to the 404 page after deployment. Say you have the following pages:
Now if you visit -> import Router from "next/router";
import { useEffect, useState } from "react";
export default function Custom404() {
const [isNotFound, setIsNotFound] = useState(false);
useEffect(() => {
// Your condition that can validate the URL
const pathNameArray = window.location.pathname.split("/");
if (pathNameArray.length <= 2) {
Router.replace(window.location.pathname); // Redirect to the right page...
} else {
setIsNotFound(true);
}
}, []);
if (isNotFound) return <h1>404 - Page Not Found</h1>;
return null;
} This will ensure the right page is rendered even if you weren't able to specify all the paths during static site generation. Even though this approach works, it will lead to browser thinking your site is throwing "404" response code so not very SEO friendly... I'd recommend this approach if you are building a native electron app / any other app that doesn't require SEO but if you are building a web app, you should go with SSR deployments... |
Beta Was this translation helpful? Give feedback.
-
How to deploy nextjs static project (pages router) with dynamic routes to S3 Setup a Custom404 page that will handle routing from client side like SPA. On S3 settings, set error page to 404.html. Also don't use trailing slash.
|
Beta Was this translation helpful? Give feedback.
-
So, not sure if it's too late to share my solution, but here is a temporary fix that might help you guys. In my current project, we are using Next.js 15.2 + Cloudfront + S3 Bucket. The next config is using Directory StructureWe maintain parallel routing structures:
On the Cloudfront side, we managed to create a custom function that checks with a regex whenever the requested URL contains a matching UUID, so it redirects to the function handler(event) {
var request = event.request;
var uri = request.uri;
// If the URI is '/', serve index.html
if (uri === '/') {
request.uri = '/index.html';
return request;
}
// Split the path into segments
var segments = uri.split('/').filter(function(segment) {
return segment.length > 0;
});
// Only replace the last segment if it's in a dynamic route pattern
if (segments.length > 0) {
var lastSegment = segments[segments.length - 1];
// Check if the segment looks like an ID/UUID
// UUID pattern: 8-4-4-4-12 hex digits
// or numeric ID pattern
var isUUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(lastSegment);
var isNumericId = /^\d+$/.test(lastSegment);
if (isUUID || isNumericId) {
// Remove the last segment and replace it with [id].html
segments.pop();
var basePath = segments.join('/');
request.uri = '/' + (basePath ? basePath + '/' : '') + '[id].html';
return request;
}
}
// Default behavior for static routes
if (uri.slice(-5) !== '.html' && uri.indexOf('.') === -1) {
request.uri = uri + '.html';
}
return request;
} The downside of using the function is, you must always use the |
Beta Was this translation helpful? Give feedback.
-
my 2 cents on this
on public folder using cloudflare, _redirects
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I would like to create a static app that I can host on S3 while using dynamic routes for some specific pages. All the data will be loaded client-side only.
I tried to do it without using getStaticPaths or by returning an empty paths array and by using
fallback
totrue
but it doesn't work.For example I would have:
index.html
products/index.html (page for /products)
products/[id].html (catch-all page for /products/1)
...
I guess the complication would also come from the routing configuration on S3 to make sure everything is routed to the right page.
Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions