Yudai Nakata
01/06/2023, 2:49 PMfunctions
directory.
Below are the steps to reproduce my problem:
npm init vite@latest repro -- --template react
cd repro
npm install
npm install -D wrangler
mkdir -p functions/api
echo "export const onRequest = c => new Response('Hello world!');" > functions/api/hello.js
npx wrangler pages dev -- npx vite
Then, running curl http://localhost:5173/api/hello
returns the scaffolded app instead of Hello world!
.
I have consulted the documentation at https://developers.cloudflare.com/pages/platform/functions/ especially the Routing section, but found no clue. I'd appreciate it if anyone could point out what I'm doing wrong.kian
01/06/2023, 3:51 PMkian
01/06/2023, 3:53 PMYudai Nakata
01/06/2023, 4:36 PMsrc/App.jsx
logs the SPA in DevTools because it uses the Vite's port. However, what I want to call here is obviously Functions' /api/hello
endpoint.
diff
- <button onClick={() => setCount((count) => count + 1)}>
+ <button onClick={() => {
+ fetch('/api/hello')
+ .then(r=>r.text())
+ .then(t=>console.log(t))
+ return setCount((count) => count + 1)
+ }}>
Quacksire
01/07/2023, 12:40 AMScript startup exceeded CPU time limit
with all of my page functions... but I don't think that a function that retunrs another fecth should cause it.
js
export async function onRequestGet(context) {
try {
if (!context.request.headers.get('X-SL-User')) {
return new Response('Not logged in', {status: 401})
}
let token = context.request.headers.get('X-SL-User').split(':')[0]
let uid = context.request.headers.get('X-SL-User').split(':')[1]
token = decodeURI(token)
let response = await fetch(`https://subdomain.schoolloop.com/mapi/assignments?studentID=${uid}`,
{
headers: {
"Authorization": `Basic ${token}`
}
}
)
response = await response.json()
return new Response(
JSON.stringify(response),
{
status: 200,
headers: {
'content-type': 'application/json',
"cache-control": `s-maxage=1200, stale-while-revalidate=600`
},
}
)
} catch (e) {
return new Response(`${e} | token: ${token} | uid: ${uid}`, {status: 500})
}
}
James
01/07/2023, 12:41 AMQuacksire
01/07/2023, 12:42 AMQuacksire
01/07/2023, 12:42 AMJames
01/07/2023, 12:42 AMQuacksire
01/07/2023, 12:43 AMQuacksire
01/07/2023, 12:43 AMJames
01/07/2023, 12:43 AMnext-on-pages
I imagine?Quacksire
01/07/2023, 12:43 AMQuacksire
01/07/2023, 12:43 AMQuacksire
01/07/2023, 12:44 AMQuacksire
01/07/2023, 12:44 AMQuacksire
01/07/2023, 12:44 AMJames
01/07/2023, 12:45 AMnext-on-pages
as the bundle size grows. https://github.com/cloudflare/next-on-pages/issues/47. https://github.com/cloudflare/next-on-pages/issues/2. etc.James
01/07/2023, 12:45 AMnext-on-pages
itself is in a state right now that I couldn't personally recommend it due to lack of maintenance and support, and would encourage you to consider an alternate solution like Netlify or Vercel for your project if you need a quick resolution. 😦Quacksire
01/07/2023, 12:46 AMQuacksire
01/07/2023, 12:46 AMJames
01/07/2023, 12:46 AMQuacksire
01/07/2023, 12:48 AMWalshy | Pages
01/07/2023, 12:49 AM[path].js
which would fire for /_sl/*
James
01/07/2023, 12:49 AM[slug].js
but that of course would match all slugs under _sl/
James
01/07/2023, 12:49 AMQuacksire
01/07/2023, 12:50 AMJames
01/07/2023, 12:50 AMJames
01/07/2023, 12:50 AM