brillout / goldpage

Page Builder.
Creative Commons Zero v1.0 Universal
57 stars 3 forks source link

Catch-all page for 404s? #16

Closed chriscalo closed 4 years ago

chriscalo commented 4 years ago

I want to have my last express handler always render 404.page.js & 404.vue. I tried using a catch-all route in my 404.page.js file:

import { fileroute, adaptContext } from "~/util/goldpage";
import page from "./404.vue";

export default {
  route: "*",
  view: page,
  title: "Viaticus | Not Found",
  renderToHtml: true,
  addInitialProps,
};

async function addInitialProps(context) {
  const { req, res, next, url } = adaptContext(context);
  return {
    url,
  };
}

But I end up getting the following error:

(node:26906) UnhandledPromiseRejectionWarning: ReferenceError: err is not defined
    at universalAdapter (/Users/chriscalo/Projects/viaticus/node_modules/@universal-adapter/express/ExpressAdapter.js:24:12)
(node:26906) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handl
ed with .catch(). (rejection id: 2)
(node:26906) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Then I tried a different approach of redirecting to /404?method=get&url=/path/that/doesn't/exist/. This works, but I don't love this approach because the URL changes. Here's my main server:

import express from "express";
import { start } from "express-start";

import authServer from "./auth";
import apiServer from "~/api";
import pagesServer from "~/pages";
import goldpageServer from "./goldpage";
import staticServer from "./static-server";
import notFoundServer from "./not-found";

export const server = express();
server.enable("trust proxy");

server.use(
  authServer,
  apiServer,
  pagesServer,
  goldpageServer,
  staticServer,
  notFoundServer,
);

if (process.mainModule === module) {
  start(server);
}

And here's not-found.js:

import path from "path";
import express from "express";

export const server = express();
export default server;

// catch-all server for all unhandled requests
// must be the last handler because it always redirects!
server.use((req, res, next) => {
  res.redirect(`/404?method=${req.method}&url=${req.url}`)
});

Is there a cleaner way to get a catch-all 404.page.js working?

brillout commented 4 years ago

HI Chris, I'll have a look tomorrow.

brillout commented 4 years ago

The wildcard route: "*" now works in the newly published version 0.5.5.

You can see an example at /examples/404-page/.

chriscalo commented 4 years ago

Thanks for the fast fix 🙏