Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
ccbikai committed Jul 21, 2024
2 parents 0d10da9 + 52187d1 commit d7fce2e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions components/dashboard/Nav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const route = useRoute()
@update:model-value="navigateTo"
>
<TabsList>
<TabsTrigger value="/dashboard">
Analysis
</TabsTrigger>
<TabsTrigger
value="/dashboard/links"
>
Links
</TabsTrigger>
<TabsTrigger value="/dashboard/analysis">
Analysis
</TabsTrigger>
</TabsList>
</Tabs>
<slot name="left" />
Expand Down
8 changes: 8 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default defineNuxtConfig({
'/dashboard/**': {
ssr: false,
},
'/dashboard': {
redirect: '/dashboard/links',
},
},

hub: {
Expand Down Expand Up @@ -49,6 +52,8 @@ export default defineNuxtConfig({
runtimeConfig: {
siteToken: 'SinkCool',
redirectStatusCode: '301',
linkCacheTtl: 60,
redirectWithQuery: false,
homeURL: '',
cfAccountId: '',
cfApiToken: '',
Expand All @@ -62,4 +67,4 @@ export default defineNuxtConfig({
},

compatibilityDate: '2024-07-08',
})
})
File renamed without changes.
1 change: 1 addition & 0 deletions server/api/link/create.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default eventHandler(async (event) => {
statusText: 'Link already exists',
})
}

else {
const expiration = getExpiration(event, link.expiration)

Expand Down
9 changes: 5 additions & 4 deletions server/middleware/1.redirect.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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)
return sendRedirect(event, homeURL)

if (slug && !reserveSlug.includes(slug) && slugRegex.test(slug) && cloudflare) {
const { KV } = cloudflare.env
const link: z.infer<typeof LinkSchema> | null = await KV.get(`link:${slug}`, { type: 'json' })
const link: z.infer<typeof LinkSchema> | null = await KV.get(`link:${slug}`, { type: 'json', cacheTtl: linkCacheTtl })
if (link) {
event.context.link = link
try {
Expand All @@ -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)
}
}
})

0 comments on commit d7fce2e

Please sign in to comment.