Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ NUXT_CF_API_TOKEN=CloudflareAPIToken
NUXT_DATASET=sink
NUXT_AI_MODEL="@cf/meta/llama-3-8b-instruct"
NUXT_AI_PROMPT="You are a URL shortening assistant......"
NUXT_ENABLE_MOBILE_DEEP_LINKS=false
NUXT_DEEP_LINK_TIMEOUT=3000
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- **Customizable Slug:** Support for personalized slugs and case sensitivity.
- **🪄 AI Slug:** Leverage AI to generate slugs.
- **Link Expiration:** Set expiration dates for your links.
- **Mobile Deep Links:** Automatically redirect mobile users to native apps when available.

## 🪧 Demo

Expand Down
12 changes: 12 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ Access statistics do not count bot traffic.
## `NUXT_API_CORS`

Set the environment variable `NUXT_API_CORS=true` during build to enable CORS support for the API.

## `NUXT_ENABLE_MOBILE_DEEP_LINKS`

Enables mobile deep link functionality. When enabled, mobile users accessing shortened links will be automatically redirected to the corresponding native app if available (e.g., Instagram app for Instagram links, Amazon app for Amazon product links).

Default: `false`

## `NUXT_DEEP_LINK_TIMEOUT`

Sets the timeout in milliseconds for deep link attempts before falling back to the web browser. This gives the system time to attempt opening the native app before redirecting to the web version.

Default: `3000` (3 seconds)
2 changes: 2 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export default defineNuxtConfig({
caseSensitive: false,
listQueryLimit: 500,
disableBotAccessLog: false,
enableMobileDeepLinks: false,
deepLinkTimeout: 3000,
public: {
previewMode: '',
slugDefaultLength: '6',
Expand Down
14 changes: 12 additions & 2 deletions server/middleware/1.redirect.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { LinkSchema } from '@@/schemas/link'
import type { z } from 'zod'
import { parsePath, withQuery } from 'ufo'
import { handleMobileDeepLink } from '../utils/mobile-deep-links'

export default eventHandler(async (event) => {
const { pathname: slug } = parsePath(event.path.replace(/^\/|\/$/g, '')) // remove leading and trailing slashes
const { slugRegex, reserveSlug } = useAppConfig(event)
const { homeURL, linkCacheTtl, redirectWithQuery, caseSensitive } = useRuntimeConfig(event)
const { homeURL, linkCacheTtl, redirectWithQuery, caseSensitive, redirectStatusCode } = useRuntimeConfig(event)
const { cloudflare } = event.context

if (event.path === '/' && homeURL)
Expand Down Expand Up @@ -36,8 +37,17 @@ export default eventHandler(async (event) => {
catch (error) {
console.error('Failed write access log:', error)
}

const target = redirectWithQuery ? withQuery(link.url, getQuery(event)) : link.url
return sendRedirect(event, target, +useRuntimeConfig(event).redirectStatusCode)

// Check for mobile deep linking
const { shouldInterceptRedirect, htmlResponse } = await handleMobileDeepLink(event, target)
if (shouldInterceptRedirect && htmlResponse) {
return htmlResponse
}

// Default redirect
return sendRedirect(event, target, +redirectStatusCode)
}
}
})
Loading