oblador / esbuild-server

Fast, lightweight and powerful development server for esbuild
MIT License
37 stars 5 forks source link

Not compatible with single page applications? #13

Closed roymrinmoy closed 1 month ago

roymrinmoy commented 11 months ago

I have tried using this plugin in a react app. It works fine when I land at localhost:3000. Routing to something like localhost:3000/dashboard also works fine. But in the routed path if I refresh the page, it just sends, "Not found". I have found the code where it happens.

// https://github.com/oblador/esbuild-server/blob/master/src/index.ts

    // Attempt to serve file from build or static directory
        for (const dir of staticDirs) {
            const staticFilePath = path_1.default.normalize(path_1.default.join(dir, url.pathname === '/' ? 'index.html' : url.pathname));
            console.log(url.pathname, staticFilePath)
            if (staticFilePath.startsWith(dir)) {
                try {
                    return await sendFile(res, staticFilePath);
                }
                catch (err) {
                    if (err.code !== 'ENOENT' && err.code !== 'EISDIR') {
                        throw err;
                    }
                }
            }
        }

It seems it is checking if a directory with the same name as the route path is available or not. If not found it returns "Not found". But in case of SPAs there will be no directory as the route name.

oblador commented 1 month ago

Pass historyApiFallback: true in options as per the README.