Khi có một yêu cầu tới một route, handler được gọi trước, sau đó tới component

Handlers in Fresh are part of the routing mechanism. They are called before the component function is called. Typically they are used to pull data from a database or another API. They can be thought of as a middleware to the current route.

export const handler = {   
    async GET(req, ctx) {     
        const data = await loadData();     // This triggers HTML rendering     
        return ctx.render(data);   
    } 
}  
export default function MyPage(props) {
    return <h1>{props.data.name}</h1> 
}

If no handler export is present, Fresh will add a default one that looks like this behind the scenes:

export const handler = {   
    GET: (req, ctx) => ctx.render() 
}

Nguồn:: Discord


Cập nhật lần cuối : 30 tháng 6, 2024
Tạo : 4 tháng 1, 2024