denoland / fresh

The next-gen web framework.
https://fresh.deno.dev
MIT License
12.05k stars 607 forks source link

Always add default GET handler #2530

Closed marvinhagemeister closed 1 week ago

marvinhagemeister commented 1 week ago

From discord: https://discord.com/channels/684898665143206084/991511118524715139/1254411831041196133

I've just been reading through some of the decisions made for Fresh 2.0. There's a lot of progress, so great work! I have one question regarding the use of the new define helpers. If a route only needs a POST handler, will we still need to define a GET handler? I did like just exporting a default page component for basic pages and then defining a POST handler. It seems (maybe I'm wrong) in the early alpha I need to define a default GET handler too (one that just returns the page) if I define a POST handler.

We should definitely add a default handler. Was an oversight on my part, there is no reason for it to not be there.

deer commented 1 week ago

Just confirming I understand the request here. Imagine I have a route like this:

import { define } from "../utils.ts";

export const handler = define.handlers({
  POST: (_ctx) => {
    console.log("i've posted");
    return new Response("POST");
  },
});

export default define.page(function PostPage() {
  return <b>"This is a basic page."</b>;
});

What we want is for visiting http://localhost:8000/post with the browser to return This is a basic page. instead of Method Not Allowed, right?

marvinhagemeister commented 1 week ago

Yup, that should be the outcome. It should also work when no define.* handler is used.