diff --git a/.env.example b/.env.example index eefd1807..7f1a2867 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,8 @@ 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_REDIRECT_WITH_QUERY=false NUXT_HOME_URL="https://sink.cool" NUXT_CF_ACCOUNT_ID=123456 NUXT_CF_API_TOKEN=CloudflareAPIToken diff --git a/components/dashboard/Nav.vue b/components/dashboard/Nav.vue index f3d418d2..2c1fa5d5 100644 --- a/components/dashboard/Nav.vue +++ b/components/dashboard/Nav.vue @@ -10,14 +10,14 @@ const route = useRoute() @update:model-value="navigateTo" > - - Analysis - Links + + Analysis + diff --git a/docs/configuration.md b/docs/configuration.md index ffb25171..a6be6883 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -14,6 +14,14 @@ 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_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 6f81ad79..144b2a7b 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -21,6 +21,9 @@ export default defineNuxtConfig({ '/dashboard/**': { ssr: false, }, + '/dashboard': { + redirect: '/dashboard/links', + }, }, hub: { @@ -49,6 +52,8 @@ export default defineNuxtConfig({ runtimeConfig: { siteToken: 'SinkCool', redirectStatusCode: '301', + linkCacheTtl: 60, + redirectWithQuery: false, homeURL: '', cfAccountId: '', cfApiToken: '', @@ -62,4 +67,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 diff --git a/server/api/link/create.post.ts b/server/api/link/create.post.ts index fa8e9cfe..77d77535 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) diff --git a/server/middleware/1.redirect.ts b/server/middleware/1.redirect.ts index cf35bf15..81f32ddc 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 } = useRuntimeConfig(event) + const { homeURL, linkCacheTtl, redirectWithQuery } = 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 { @@ -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) } } })