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.SvelteKit docs
Cloudflare Pages • SvelteKit documentation
5 Replies
Here's all my code... https://github.com/adamb/selfie
+server.js/ts
)hello.js
to hello/+server.js
wow! that works. Nice if it was documented somewhere.
Functions should be implemented as server endpoints in your SvelteKit app.
SvelteKit docs
Routing • SvelteKit documentation
thanks! I apprecaite it. I looked at that page but didn't grok that far down.