Comments (2)
To try to summarize the situation concisely for anyone who is trying to understand this GitHub Issue, the main goal here is to have a way for static sites to serve up a 404 page using an elm-pages
Route (ideally the ErrorPage
NotFound
rendering, which currently only works for server-rendered error pages).
One of the core challenges here is that elm-pages
makes some assumptions that the URL it is being loaded at is from the static files it generates. If you use redirects or nginx rules, etc. to serve up something like /not-found
when the actual URL that is visited is /this/url/does/not/exist
, then elm-pages
tries to resolve the URL using its routing so when it hydrates it can cause errors.
from elm-pages.
My work-around for this, for now, is adding this logic to my elm-pages.config.mjs
adapter function:
//
// Hack: Work around an Elm Pages bug in our 404 page by removing its script tags.
//
// The bug: ErrorPage is shown as-is very briefly, but upon the Elm app hydrating and running, it
// decides that the current route has no matches, and the routing logic hits a fallback that makes
// it display a hard-coded "This page could not be found" message, instead of our ErrorPage.
//
// Search `.elm-pages/Main.elm` for "This page could" to see the (generated) code responsible.
//
// Issue: https://github.com/dillonkearns/elm-pages/issues/432
//
// We can't solve this by making the page a root-level "splat", either:
// https://github.com/dillonkearns/elm-pages/issues/407
//
// Related: https://github.com/dillonkearns/elm-pages/issues/397
//
const notFoundPath = "dist/page-not-found/index.html";
const html = await readFile(notFoundPath, "utf-8");
// The tags look like this:
// <script defer src="/elm.fcba9bad.js" type="text/javascript"></script>
// <script type="module" crossorigin src="/assets/index-06bd4283.js"></script>
const fixedHtml = html
.replace(
/<script defer src="\/elm\..{8}\.js" type="text\/javascript"><\/script>/,
""
)
.replace(
/<script type="module" crossorigin src="\/assets\/index-.{8}\.js"><\/script>/,
""
);
await writeFile(notFoundPath, fixedHtml, "utf-8");
Here's the import for the file functions:
import { readFile, writeFile } from "node:fs/promises";
from elm-pages.
Related Issues (20)
- elm-pages run script/src/AddRoute.elm fails when run before first build HOT 4
- [SSCCE] ANSI escape codes in `onError` and `ErrorPage` error messages
- Replace StaticHttp examples in BackendTask HOT 1
- Cannot watch for fragment changes in URL
- Scroll is reset on back navigation HOT 1
- DeadCodeEliminateData errors cached in elm-stuff
- Feature request: `Pages.Script.writeFileBinary`/`BackendTask.File.rawBinary` HOT 2
- `elm-pages build` inserts `import FatalError` into unit test modules HOT 2
- Update .gitignore generator template for elm-pages-cli-mjs HOT 1
- Compilation error when using `elm-pages docs` HOT 9
- npx elm-pages run <script name> does not work on Windows HOT 1
- Vite Pre-transform error after upgrade HOT 9
- Initialization failed while loading node modules HOT 4
- Compatibility Keys match, but elm-pages says they don't when running script HOT 7
- Feature request: `BackendTask.race` HOT 1
- Feature request: `Script.stateful` HOT 1
- `Debug.todo` causes a `Script` to stop with no error message HOT 5
- INTERNAL ERROR when calling custom task. HOT 1
- BadBody: INTERNAL ERROR when input to Backend.Custom.run is mutated HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elm-pages.