Isaac McFadyen | YYZ01
03/15/2022, 2:33 PMIsaac McFadyen | YYZ01
03/15/2022, 2:33 PMGreg Brimble | Cloudflare Pages
03/15/2022, 2:34 PMIsaac McFadyen | YYZ01
03/15/2022, 2:39 PMn
03/15/2022, 4:11 PMn
03/15/2022, 4:12 PMrishav
03/15/2022, 4:24 PMreturn new Response('<p>Hello, World</p>');
Isaac McFadyen | YYZ01
03/15/2022, 4:27 PMtext/html
rishav
03/15/2022, 4:31 PMrishav
03/15/2022, 6:04 PMWalshy | Pages
03/15/2022, 6:06 PMWalshy | Pages
03/15/2022, 6:06 PMrishav
03/15/2022, 6:13 PMβββ ...
βββ functions
| βββ api
β βββ [[path]].ts
β βββ [id].ts
β βββ index.ts
βββ ...
The following routes will be generated based on the file structure, mapping the URL pattern to the /functions file that will be invoked:
/api/todos => ./functions/api/todos/index.ts
/api/todos/* => ./functions/api/todos/[id].ts
/api/todos/*/** => ./functions/api/todos/[[path]].ts
[[path]]
and index
routes can coexist. However, in practice, the [[path]] route always overrides the index one.
I am on wragler next and observed this on local hostingrishav
03/15/2022, 7:02 PMnpx wrangler pages dev ./pub
and I am serving a style.css
from the pub directory - if I also define a top level route like
./functions
|-- todos
|-- [[catchall]].js
then every request for http://localhost:8788/pub/style.css
also returns the Response from my [[catchall]].js
Isaac McFadyen | YYZ01
03/15/2022, 7:03 PMIsaac McFadyen | YYZ01
03/15/2022, 7:03 PMIsaac McFadyen | YYZ01
03/15/2022, 7:03 PMnext()
rishav
03/15/2022, 7:15 PMIsaac McFadyen | YYZ01
03/15/2022, 7:16 PMIsaac McFadyen | YYZ01
03/15/2022, 7:16 PMexport async function onRequest(context) {
// Contents of context object
const {
request, // same as existing Worker API
env, // same as existing Worker API
params, // if filename includes [id] or [[path]]
waitUntil, // same as ctx.waitUntil in existing Worker API
next, // used for middleware or to fetch assets
data, // arbitrary space for passing data between middlewares
} = context;
}
Isaac McFadyen | YYZ01
03/15/2022, 7:16 PMIsaac McFadyen | YYZ01
03/15/2022, 7:16 PMIsaac McFadyen | YYZ01
03/15/2022, 7:17 PMconst response = await context.next();
return response
because next()
gives you the static asset, if one exists.rishav
03/15/2022, 7:40 PMexport async function onRequest(context) {
const assets = await context.next();
console.log (assets)
const html = /*html*/`
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href=`WHAT SHOULD GO HERE??` rel="stylesheet">
</head>
<body>
<h1 class="text-3xl font-bold underline text-red-900">
HOME
</h1>
</body>
</html>
`;
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
});
}
I am not sure how to go about using the style.css from my pub folder in my html code.Isaac McFadyen | YYZ01
03/15/2022, 7:49 PM/pub
or not. That'll help you decide what to serve.Isaac McFadyen | YYZ01
03/15/2022, 7:50 PM/pub
, and if so, do:
const assets = await next();
return assets;
Otherwise, return your HTML.Isaac McFadyen | YYZ01
03/15/2022, 7:51 PMexport async function onRequest(context) {
const url = new URL(context.request.url);
if (url.pathname.startsWith('/pub/')) {
return await context.next();
}
const html = /*html*/`
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href=`WHAT SHOULD GO HERE??` rel="stylesheet">
</head>
<body>
<h1 class="text-3xl font-bold underline text-red-900">
HOME
</h1>
</body>
</html>
`;
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
});
}
rishav
03/15/2022, 8:03 PMAngius
03/15/2022, 11:28 PMsyrokos
03/16/2022, 12:43 AM