ab90..
ab90..
CDCloudflare Developers
Created by ab90.. on 9/4/2024 in #pages-help
I can't for the life of me get a svelte project to call a function.
I've tried putting the function code in routes/hello.js and routes/hello.server.js According to the docs: https://kit.svelte.dev/docs/adapter-cloudflare#notes Functions contained in the /functions directory at the project's root will not be included in the deployment, which is compiled to a single _worker.js file. Functions should be implemented as server endpoints in your SvelteKit app. My "function" code is: routes/hello.js: // Handle GET requests export const GET = () => { return new Response('hello ' + String(Math.random())); }; and in routes/+page.svelte: async function handleHelloSubmit(event) { event.preventDefault(); try { const response = await fetch('/hello'); if (!response.ok) { throw new Error('Failed to fetch hello message'); } helloMessage = await response.text(); } catch (error) { console.error('Error fetching hello message:', error); helloMessage = 'An error occurred while fetching the hello message.'; } } I don't get it. npm run build works and wrangler pages deploy too. I've deployed it to https://selfie.pages.dev/ so https://selfie.pages.dev/hello returns 404. Any ideas would be greatly apprecaited. I'm just trying to get this scaffolding working so I can write the real functions... But I'm stuck on just calling functions from Svelte.
10 replies