Skip to content

Commit

Permalink
Improve universal link referencing and performance (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushchris authored Sep 6, 2024
1 parent 08b40c2 commit a180ec4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 4 additions & 1 deletion apps/platform/src/organizations/OrganizationMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Context } from 'koa'
import { getOrganization, getOrganizationByUsername } from './OrganizationService'
import { getDefaultOrganization, getOrganization, getOrganizationByUsername } from './OrganizationService'
import App from '../app'

export const organizationMiddleware = async (ctx: Context, next: () => void) => {
const organizationId = ctx.cookies.get('organization', { signed: true })
Expand All @@ -8,6 +9,8 @@ export const organizationMiddleware = async (ctx: Context, next: () => void) =>
} else if (ctx.subdomains && ctx.subdomains[0]) {
const subdomain = ctx.subdomains[0]
ctx.state.organization = await getOrganizationByUsername(subdomain)
} else if (App.main.env.config.multiOrg) {
ctx.state.organization = await getDefaultOrganization()
}
return next()
}
17 changes: 14 additions & 3 deletions apps/platform/src/render/LinkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Router from '@koa/router'
import App from '../app'
import { encodedLinkToParts, trackMessageEvent } from './LinkService'
import Organization from '../organizations/Organization'
import { cacheGet, cacheSet } from '../config/redis'

const router = new Router<{
app: App
Expand Down Expand Up @@ -30,14 +31,24 @@ router.get('/o', async ctx => {
})

router.get('/.well-known/:file', async ctx => {
const url = ctx.state.organization?.tracking_deeplink_mirror_url
const organization = ctx.state.organization
const url = organization?.tracking_deeplink_mirror_url
const file = ctx.params.file
if (!url) {
ctx.status = 404
return
}
const response = await fetch(`${url}/.well-known/${file}`)
ctx.body = await response.json()

const key = `well-known:${organization.id}:${file}`
const value = await cacheGet<any>(App.main.redis, key)
if (value) {
ctx.body = value
} else {
const response = await fetch(`${url}/.well-known/${file}`)
const value = await response.json()
await cacheSet(App.main.redis, key, value, 60 * 60 * 5)
ctx.body = value
}
})

export default router

0 comments on commit a180ec4

Please sign in to comment.