From 6459d07ce579b0fc78cbd1f93370f6467f639415 Mon Sep 17 00:00:00 2001 From: Jason Schwarzenberger Date: Fri, 4 Dec 2020 16:16:05 +1300 Subject: [PATCH] make submit form not require JS. --- webapp/package.json | 1 + webapp/src/routes/[id].json.js | 2 +- webapp/src/routes/index.json.js | 2 +- webapp/src/routes/search.json.js | 2 +- webapp/src/routes/submit.json.js | 13 +++++++++---- webapp/src/routes/submit.svelte | 8 ++++++-- webapp/src/server.js | 5 +++-- webapp/static/manifest.json | 6 +++--- webapp/yarn.lock | 5 +++++ 9 files changed, 30 insertions(+), 14 deletions(-) diff --git a/webapp/package.json b/webapp/package.json index e613c51..157cdcb 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -9,6 +9,7 @@ "start": "node __sapper__/build" }, "dependencies": { + "@polka/redirect": "^1.0.0-next.0", "body-parser": "^1.19.0", "compression": "^1.7.1", "date-fns": "^2.16.1", diff --git a/webapp/src/routes/[id].json.js b/webapp/src/routes/[id].json.js index 2a958e1..df6a44a 100644 --- a/webapp/src/routes/[id].json.js +++ b/webapp/src/routes/[id].json.js @@ -6,7 +6,7 @@ const API_URL = process.env.API_URL || 'http://localhost:33842'; export async function get(req, res) { const response = await fetch(`${API_URL}/api/${req.params.id}`); - res.writeHead(response.status, { 'Content-Type': 'application/json' }); + res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') }); if (!response.ok) { return res.end(await response.text()); } diff --git a/webapp/src/routes/index.json.js b/webapp/src/routes/index.json.js index f8c3950..1638e25 100644 --- a/webapp/src/routes/index.json.js +++ b/webapp/src/routes/index.json.js @@ -10,7 +10,7 @@ export async function get(req, res) { limit: req.query.limit || 20, }; const response = await fetch(`${API_URL}/api?skip=${skip}&limit=${limit}`); - res.writeHead(response.status, { 'Content-Type': 'application/json' }); + res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') }); if (!response.ok) { return res.end(await response.text()); } diff --git a/webapp/src/routes/search.json.js b/webapp/src/routes/search.json.js index 784efe6..7421637 100644 --- a/webapp/src/routes/search.json.js +++ b/webapp/src/routes/search.json.js @@ -10,7 +10,7 @@ export async function get(req, res) { limit: req.query.limit || 20, }; const response = await fetch(`${API_URL}/api/search?q=${req.query.q}&skip=${skip}&limit=${limit}`); - res.writeHead(response.status, { 'Content-Type': 'application/json' }); + res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') }); if (!response.ok) { return res.end(await response.text()); } diff --git a/webapp/src/routes/submit.json.js b/webapp/src/routes/submit.json.js index 1e7dd82..2369e79 100644 --- a/webapp/src/routes/submit.json.js +++ b/webapp/src/routes/submit.json.js @@ -1,12 +1,17 @@ import FormData from 'form-data'; import fetch from 'isomorphic-fetch'; +import redirect from '@polka/redirect'; const API_URL = process.env.API_URL || 'http://localhost:33842'; export async function post(req, res) { - const data = new FormData(); - data.append('url', req.body.url); - const response = await fetch(`${API_URL}/api/submit`, { method: "POST", body: data }); - res.writeHead(response.status, { 'Content-Type': 'application/json' }); + const body = new FormData(); + body.append('url', req.body.url); + const response = await fetch(`${API_URL}/api/submit`, { method: "POST", body }); + if (req.body.redirect) { + const { nid } = await response.json(); + return redirect(res, 302, `/${nid}`); + } + res.writeHead(response.status, { 'Content-Type': response.headers.get('Content-Type') }); res.end(await response.text()); } \ No newline at end of file diff --git a/webapp/src/routes/submit.svelte b/webapp/src/routes/submit.svelte index 656ed54..64b4173 100644 --- a/webapp/src/routes/submit.svelte +++ b/webapp/src/routes/submit.svelte @@ -127,7 +127,11 @@ width="200" height="200" /> -
+ - +

Something went wrong.

diff --git a/webapp/src/server.js b/webapp/src/server.js index f6f93ef..ee02796 100644 --- a/webapp/src/server.js +++ b/webapp/src/server.js @@ -2,14 +2,15 @@ import sirv from 'sirv'; import polka from 'polka'; import compression from 'compression'; import * as sapper from '@sapper/server'; -import { json } from 'body-parser'; +import { json, urlencoded } from 'body-parser'; const { PORT, NODE_ENV } = process.env; const dev = NODE_ENV === 'development'; -polka() // You can also use Express +polka() .use( json(), + urlencoded(), compression({ threshold: 0 }), sirv('static', { dev }), sapper.middleware(), diff --git a/webapp/static/manifest.json b/webapp/static/manifest.json index 78ad114..f5ce2c1 100644 --- a/webapp/static/manifest.json +++ b/webapp/static/manifest.json @@ -1,8 +1,8 @@ { "background_color": "#ffffff", "theme_color": "#333333", - "name": "TODO", - "short_name": "TODO", + "name": "Qot. news", + "short_name": "Qot. news", "display": "minimal-ui", "start_url": "/", "icons": [ @@ -17,4 +17,4 @@ "type": "image/png" } ] -} +} \ No newline at end of file diff --git a/webapp/yarn.lock b/webapp/yarn.lock index 59e9c55..30d4794 100644 --- a/webapp/yarn.lock +++ b/webapp/yarn.lock @@ -2,6 +2,11 @@ # yarn lockfile v1 +"@polka/redirect@^1.0.0-next.0": + version "1.0.0-next.0" + resolved "https://registry.yarnpkg.com/@polka/redirect/-/redirect-1.0.0-next.0.tgz#df56f2fc3f1db9c5397c7eafc0e85442eeead427" + integrity sha512-ym6ooqMr09+cV+y52p5kszJ0jYcX+nJfm8POrQb7QYowvpPPuneZ71EclHrQSB7a50lcytgR/xtL6AUFdvyEkg== + "@polka/url@^1.0.0-next.11", "@polka/url@^1.0.0-next.9": version "1.0.0-next.11" resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.11.tgz#aeb16f50649a91af79dbe36574b66d0f9e4d9f71"