Skip to content

[v2.0] Object version of handler not supported for _error, _500 and _404 #2953

@csvn

Description

@csvn

While migrating a page from Fresh v1 to v2-alpha, I noticed that the handler stopped working for what was previously _404 in the new _error.

The issue seems to stem from using the objectMethod type of handler over just a function for GET:

export const handler = define.handlers({
  GET(ctx) {
    // Return 404 with no HTML body if URL looks to be file-like
    const is404 = ctx.error instanceof HttpError && ctx.error.status === 404;
    const isFileLike = fileExtensions.some((ext) => ctx.url.pathname.endsWith(ext));

    if (is404  && isFileLike) return new Response('', { status: 404, statusText: 'Not found' });

    return page();
  }
});

// When changed to the below style it started working:
export const handler = define.handlers((ctx) => {
  // Same method body
});

There are two "FIXME"'s in the code that is most likely the cause of this issue:

Solution

A quick fix would be to log a warning until the issue is fixed.

  • Should _404 support any other handler types than GET?
  • Does _500/_error support methods other than GET?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions