From 09a97070d35fed65f4830ae993ea19fde7343fc2 Mon Sep 17 00:00:00 2001 From: dr-data Date: Mon, 15 Jul 2024 00:12:12 +0800 Subject: [PATCH 1/7] link first --- components/dashboard/Nav.vue | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/components/dashboard/Nav.vue b/components/dashboard/Nav.vue index f3d418d..64fcd37 100644 --- a/components/dashboard/Nav.vue +++ b/components/dashboard/Nav.vue @@ -10,14 +10,15 @@ const route = useRoute() @update:model-value="navigateTo" > - - Analysis - Links + + Analysis + + From 21d8352de07c5d349ef0d74eb8112dda6393b19c Mon Sep 17 00:00:00 2001 From: dr-data Date: Mon, 15 Jul 2024 00:16:45 +0800 Subject: [PATCH 2/7] link first --- server/api/link/create.post.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/server/api/link/create.post.ts b/server/api/link/create.post.ts index fa8e9cf..ba75e68 100644 --- a/server/api/link/create.post.ts +++ b/server/api/link/create.post.ts @@ -12,6 +12,7 @@ export default eventHandler(async (event) => { statusText: 'Link already exists', }) } + else { const expiration = getExpiration(event, link.expiration) From ca12fdd8768069788b44c584a8b3d109d9811acf Mon Sep 17 00:00:00 2001 From: dr-data Date: Mon, 15 Jul 2024 00:20:15 +0800 Subject: [PATCH 3/7] link first --- server/api/link/create.post.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/api/link/create.post.ts b/server/api/link/create.post.ts index ba75e68..a52d703 100644 --- a/server/api/link/create.post.ts +++ b/server/api/link/create.post.ts @@ -12,7 +12,7 @@ export default eventHandler(async (event) => { statusText: 'Link already exists', }) } - + else { const expiration = getExpiration(event, link.expiration) @@ -24,5 +24,6 @@ export default eventHandler(async (event) => { }) setResponseStatus(event, 201) return { link } + } }) From 8f8865801acbcef3de633598233771406867d512 Mon Sep 17 00:00:00 2001 From: ccbikai Date: Sat, 20 Jul 2024 18:11:38 +0800 Subject: [PATCH 4/7] feat: redirect dashboard to analysis and update nav link Enhances navigation by redirecting the main dashboard to the analysis page and updating the navigation link to directly access the analysis section. This change streamlines user access to the primary dashboard functionality. --- components/dashboard/Nav.vue | 3 +-- nuxt.config.ts | 5 ++++- pages/dashboard/{index.vue => analysis.vue} | 0 3 files changed, 5 insertions(+), 3 deletions(-) rename pages/dashboard/{index.vue => analysis.vue} (100%) diff --git a/components/dashboard/Nav.vue b/components/dashboard/Nav.vue index 64fcd37..2c1fa5d 100644 --- a/components/dashboard/Nav.vue +++ b/components/dashboard/Nav.vue @@ -15,10 +15,9 @@ const route = useRoute() > Links - + Analysis - diff --git a/nuxt.config.ts b/nuxt.config.ts index 6f81ad7..c7685f9 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -21,6 +21,9 @@ export default defineNuxtConfig({ '/dashboard/**': { ssr: false, }, + '/dashboard': { + redirect: '/dashboard/links', + }, }, hub: { @@ -62,4 +65,4 @@ export default defineNuxtConfig({ }, compatibilityDate: '2024-07-08', -}) \ No newline at end of file +}) diff --git a/pages/dashboard/index.vue b/pages/dashboard/analysis.vue similarity index 100% rename from pages/dashboard/index.vue rename to pages/dashboard/analysis.vue From 3c0a7be6eb3f48f212d85aa04630662f357bf1e0 Mon Sep 17 00:00:00 2001 From: ccbikai Date: Sat, 20 Jul 2024 18:13:05 +0800 Subject: [PATCH 5/7] style: remove unnecessary newline for cleaner code --- server/api/link/create.post.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/server/api/link/create.post.ts b/server/api/link/create.post.ts index a52d703..77d7753 100644 --- a/server/api/link/create.post.ts +++ b/server/api/link/create.post.ts @@ -24,6 +24,5 @@ export default eventHandler(async (event) => { }) setResponseStatus(event, 201) return { link } - } }) From 2876385f20c3b21511bfd6a38acbf67cfeb0e1bd Mon Sep 17 00:00:00 2001 From: ccbikai Date: Sat, 20 Jul 2024 18:21:40 +0800 Subject: [PATCH 6/7] feat: add link cache TTL for performance optimization Improves response times by introducing a configurable link cache TTL, defaulting to 60 seconds, to ensure quick access to frequently requested links while maintaining responsiveness to updates. --- .env.example | 1 + docs/configuration.md | 4 ++++ nuxt.config.ts | 1 + server/middleware/1.redirect.ts | 4 ++-- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index eefd180..b1f341d 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,7 @@ NUXT_PUBLIC_PREVIEW_MODE=true NUXT_PUBLIC_SLUG_DEFAULT_LENGTH=5 NUXT_SITE_TOKEN=SinkCool NUXT_REDIRECT_STATUS_CODE=308 +NUXT_LINK_CACHE_TTL=60 NUXT_HOME_URL="https://sink.cool" NUXT_CF_ACCOUNT_ID=123456 NUXT_CF_API_TOKEN=CloudflareAPIToken diff --git a/docs/configuration.md b/docs/configuration.md index ffb2517..47669be 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -14,6 +14,10 @@ Sets the default length of the generated SLUG. Redirects default to use HTTP 301 status code, you can set it to `302`/`307`/`308`. +## `NUXT_LINK_CACHE_TTL` + +Cache links can speed up access, but setting them too long may result in slow changes taking effect. The default value is 60 seconds. + ## `NUXT_HOME_URL` The default Sink homepage is the introduction page, you can replace it with your own website. diff --git a/nuxt.config.ts b/nuxt.config.ts index c7685f9..a071081 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -52,6 +52,7 @@ export default defineNuxtConfig({ runtimeConfig: { siteToken: 'SinkCool', redirectStatusCode: '301', + linkCacheTtl: 60, homeURL: '', cfAccountId: '', cfApiToken: '', diff --git a/server/middleware/1.redirect.ts b/server/middleware/1.redirect.ts index cf35bf1..327f2af 100644 --- a/server/middleware/1.redirect.ts +++ b/server/middleware/1.redirect.ts @@ -5,7 +5,7 @@ import type { LinkSchema } from '@/schemas/link' export default eventHandler(async (event) => { const { pathname: slug } = parsePath(event.path.slice(1)) // remove leading slash const { slugRegex, reserveSlug } = useAppConfig(event) - const { homeURL } = useRuntimeConfig(event) + const { homeURL, linkCacheTtl } = useRuntimeConfig(event) const { cloudflare } = event.context if (event.path === '/' && homeURL) @@ -13,7 +13,7 @@ export default eventHandler(async (event) => { if (slug && !reserveSlug.includes(slug) && slugRegex.test(slug) && cloudflare) { const { KV } = cloudflare.env - const link: z.infer | null = await KV.get(`link:${slug}`, { type: 'json' }) + const link: z.infer | null = await KV.get(`link:${slug}`, { type: 'json', cacheTtl: linkCacheTtl }) if (link) { event.context.link = link try { From 52187d1ff694c75a4a900a15f52a4d06f7409f4b Mon Sep 17 00:00:00 2001 From: ccbikai Date: Sat, 20 Jul 2024 18:34:49 +0800 Subject: [PATCH 7/7] feat: disable query string redirection by default Enhances security and performance by preventing query strings from being carried over during redirection, aligning with best practices. --- .env.example | 1 + docs/configuration.md | 4 ++++ nuxt.config.ts | 1 + server/middleware/1.redirect.ts | 7 ++++--- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index b1f341d..7f1a286 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,7 @@ NUXT_PUBLIC_SLUG_DEFAULT_LENGTH=5 NUXT_SITE_TOKEN=SinkCool NUXT_REDIRECT_STATUS_CODE=308 NUXT_LINK_CACHE_TTL=60 +NUXT_REDIRECT_WITH_QUERY=false NUXT_HOME_URL="https://sink.cool" NUXT_CF_ACCOUNT_ID=123456 NUXT_CF_API_TOKEN=CloudflareAPIToken diff --git a/docs/configuration.md b/docs/configuration.md index 47669be..a6be688 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -18,6 +18,10 @@ Redirects default to use HTTP 301 status code, you can set it to `302`/`307`/`30 Cache links can speed up access, but setting them too long may result in slow changes taking effect. The default value is 60 seconds. +## `NUXT_REDIRECT_WITH_QUERY` + +URL parameters are not carried during link redirection by default and it is not recommended to enable this feature. + ## `NUXT_HOME_URL` The default Sink homepage is the introduction page, you can replace it with your own website. diff --git a/nuxt.config.ts b/nuxt.config.ts index a071081..144b2a7 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -53,6 +53,7 @@ export default defineNuxtConfig({ siteToken: 'SinkCool', redirectStatusCode: '301', linkCacheTtl: 60, + redirectWithQuery: false, homeURL: '', cfAccountId: '', cfApiToken: '', diff --git a/server/middleware/1.redirect.ts b/server/middleware/1.redirect.ts index 327f2af..81f32dd 100644 --- a/server/middleware/1.redirect.ts +++ b/server/middleware/1.redirect.ts @@ -1,11 +1,11 @@ import type { z } from 'zod' -import { parsePath } from 'ufo' +import { parsePath, withQuery } from 'ufo' import type { LinkSchema } from '@/schemas/link' export default eventHandler(async (event) => { const { pathname: slug } = parsePath(event.path.slice(1)) // remove leading slash const { slugRegex, reserveSlug } = useAppConfig(event) - const { homeURL, linkCacheTtl } = useRuntimeConfig(event) + const { homeURL, linkCacheTtl, redirectWithQuery } = useRuntimeConfig(event) const { cloudflare } = event.context if (event.path === '/' && homeURL) @@ -22,7 +22,8 @@ export default eventHandler(async (event) => { catch (error) { console.error('Failed write access log:', error) } - return sendRedirect(event, link.url, +useRuntimeConfig(event).redirectStatusCode) + const target = redirectWithQuery ? withQuery(link.url, getQuery(event)) : link.url + return sendRedirect(event, target, +useRuntimeConfig(event).redirectStatusCode) } } })