Giter VIP home page Giter VIP logo

Comments (2)

dillonkearns avatar dillonkearns commented on June 1, 2024 1

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.

adamdicarlo0 avatar adamdicarlo0 commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.