Skip to content

Commit a37f3b3

Browse files
Alternative fix for #3952 (#3970)
* [fix] make demo app work without JS * alternative approach to #3965 * actually Co-authored-by: Geoff Rich <[email protected]>
1 parent 922eadf commit a37f3b3

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

.changeset/calm-spiders-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': patch
3+
---
4+
5+
make demo app work without JS

packages/create-svelte/templates/default/src/routes/todos/_api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
This module is used by the /todos endpoint to
2+
This module is used by the /todos endpoint to
33
make calls to api.svelte.dev, which stores todos
44
for each user. The leading underscore indicates that this is
55
a private module, _not_ an endpoint — visiting /todos/_api
@@ -11,9 +11,9 @@
1111

1212
const base = 'https://api.svelte.dev';
1313

14-
export async function api(request: Request, resource: string, data?: Record<string, unknown>) {
14+
export function api(method: string, resource: string, data?: Record<string, unknown>) {
1515
return fetch(`${base}/${resource}`, {
16-
method: request.method,
16+
method,
1717
headers: {
1818
'content-type': 'application/json'
1919
},

packages/create-svelte/templates/default/src/routes/todos/index.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { api } from './_api';
22
import type { RequestHandler } from '@sveltejs/kit';
33

4-
export const get: RequestHandler = async ({ request, locals }) => {
4+
export const get: RequestHandler = async ({ locals }) => {
55
// locals.userid comes from src/hooks.js
6-
const response = await api(request, `todos/${locals.userid}`);
6+
const response = await api('get', `todos/${locals.userid}`);
77

88
if (response.status === 404) {
99
// user hasn't created a todo list.
@@ -15,7 +15,7 @@ export const get: RequestHandler = async ({ request, locals }) => {
1515
};
1616
}
1717

18-
if (response.ok) {
18+
if (response.status === 200) {
1919
return {
2020
body: {
2121
todos: await response.json()
@@ -31,22 +31,37 @@ export const get: RequestHandler = async ({ request, locals }) => {
3131
export const post: RequestHandler = async ({ request, locals }) => {
3232
const form = await request.formData();
3333

34-
return api(request, `todos/${locals.userid}`, {
34+
await api('post', `todos/${locals.userid}`, {
3535
text: form.get('text')
3636
});
37+
38+
return {};
39+
};
40+
41+
// If the user has JavaScript disabled, the URL will change to
42+
// include the method override unless we redirect back to /todos
43+
const redirect = {
44+
status: 303,
45+
headers: {
46+
location: '/todos'
47+
}
3748
};
3849

3950
export const patch: RequestHandler = async ({ request, locals }) => {
4051
const form = await request.formData();
4152

42-
return api(request, `todos/${locals.userid}/${form.get('uid')}`, {
53+
await api('patch', `todos/${locals.userid}/${form.get('uid')}`, {
4354
text: form.has('text') ? form.get('text') : undefined,
4455
done: form.has('done') ? !!form.get('done') : undefined
4556
});
57+
58+
return redirect;
4659
};
4760

4861
export const del: RequestHandler = async ({ request, locals }) => {
4962
const form = await request.formData();
5063

51-
return api(request, `todos/${locals.userid}/${form.get('uid')}`);
64+
await api('delete', `todos/${locals.userid}/${form.get('uid')}`);
65+
66+
return redirect;
5267
};

0 commit comments

Comments
 (0)