Explore why fallback pages matter for Lovable SPAs, how to set up fallback routing, and best practices for single-page app performance.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Understanding Fallback Pages in Lovable SPAs
How the Fallback Process Works in a SPA
Example of a Fallback Code Snippet in a SPA
/_ Imagine this is part of the routing logic in a SPA _/
function handleRoute(route) {
if (routes[route]) {
// Load and display the correct page
displayPage(routes[route]);
} else {
// Unknown route: fall back to the safe page
displayFallbackPage();
}
}
Why Fallback Pages Are Important for Users
Step One: Create the Configuration File for Fallback Routing
lovable.config.json
in the root directory of your project. This file is where Lovable looks for custom configuration rules.
lovable.config.json
. This code tells Lovable to serve the index.html
file no matter what URL a user visits (unless the file exists in the designated folder). In this example, it is assumed that your static files (like index.html
) are in a folder named public
:
{
"routes": [
{
"src": "/public/(.\*)",
"dest": "/public/$1"
},
{
"src": "/(.\*)",
"dest": "/public/index.html"
}
]
}
public
) will be delivered as expected. Any other route (for example, /about
or /contact
) will return index.html
. This is the core of the fallback routing for your single-page application (SPA).
Step Two: Organize Your Application Files
index.html
, is placed inside a folder called public
in your project root. This helps the configuration correctly reference the file.
public
folder or in appropriate subdirectories. This way, the first route rule (for explicit static files) will work without a hitch.
Step Three: Review and Finalize Your Code Changes
lovable.config.json
is located at the root level of your project.
/your-project-root
/public
index.html
(other assets)
lovable.config.json
Step Four: Understand the Fallback Routing Logic
lovable.config.json
checks if the URL starts with /public/
. If it does, then Lovable will serve the requested static file.
/(.\*)
) and directs it to /public/index.html
. This is essential for single-page applications because it lets the client-side code handle routing, even when a user refreshes the page or directly enters a URL.
Configuring Fallback Routing in Lovable for Single-Page Applications
server.js
. This file will control how your application handles all incoming requests.server.js
and add the following code. This code uses a popular module to serve your files and then “falls back” to sending your main index.html
when no other file is found. This is essential for Single-Page Applications (SPA) because it allows the client-side JavaScript to manage routing:
// server.js
const express = require('express');
const path = require('path');
const app = express();
// This tells the server where your static files are located.
// Create a folder named 'public' in your project and place your index.html and other assets there.
app.use(express.static(path.join(__dirname, 'public')));
// Fallback Routing
// Any route that is not recognized as a static file will trigger this middleware.
// The server sends back index.html, allowing your SPA's client-side routing to take over.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// This starts your server on port 3000 (or a port defined in the environment).
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(Server is running on port ${PORT}
);
});
Defining Your Project Dependencies
package.json
in the root of your project. This file tells Lovable which external code modules your app requires.package.json
in the code editor and paste the following code. Note that this lists the dependency for the server module we used. Lovable will use this file to manage your dependencies automatically since it does not provide a terminal for manual installation commands:
{
"name": "lovable-spa",
"version": "1.0.0",
"description": "A Single-Page Application with fallback routing for Lovable",
"main": "server.js",
"dependencies": {
"express": "^4.18.0"
},
"scripts": {
"start": "node server.js"
}
}
server.js
file.
Placing Your SPA Entry Point
public
in your project’s root directory. This folder will store all static assets for your SPA.public
folder, ensure that you have an index.html
file. This file acts as the starting point for your application and will be served for all routes not recognized by your backend logic.server.js
will always point to this index.html
, allowing your client-side JavaScript to manage the navigation and display the correct views.
Best Practices and Troubleshooting Tips
public
folder) is placed before the fallback route. This prevents valid asset requests from being intercepted by the fallback.index.html
in the fallback route is accurate.server.js
and package.json
updated and error-free is key to a maintainable project.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.