Skip to content

Commit

Permalink
Use defaults in preview handler
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Nov 14, 2023
1 parent 567ad0c commit 121ff48
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/core/driver/NextDriver.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ class NextDriver extends DefaultDriver implements NextApi {
apiKey = process.env.ALINEA_API_KEY
jwtSecret = this.apiKey || 'dev'

async resolver(): Promise<Resolver> {
async getDefaults(): Promise<ResolveDefaults> {
const {cookies, draftMode} = await import('next/headers.js')
const [draftStatus] = outcome(() => draftMode())
const isDraft = draftStatus?.isEnabled
const devUrl = process.env.ALINEA_DEV_SERVER
const resolveDefaults: ResolveDefaults = {
realm: Realm.Published
}
Expand All @@ -52,6 +51,12 @@ class NextDriver extends DefaultDriver implements NextApi {
if (entryId && phase) resolveDefaults.preview = {entryId, phase, update}
}
}
return resolveDefaults
}

async resolver(): Promise<Resolver> {
const resolveDefaults = await this.getDefaults()
const devUrl = process.env.ALINEA_DEV_SERVER
if (devUrl)
return new Client({
config: this.config,
Expand Down Expand Up @@ -94,11 +99,12 @@ class NextDriver extends DefaultDriver implements NextApi {
cookies().delete(PREVIEW_PHASE_NAME)
}
const cnx = (await this.resolver()) as Client
const resolveDefaults = await this.getDefaults()
const url = (await cnx.resolve({
...resolveDefaults,
selection: Selection.create(
Entry({entryId: params.entryId}).select(Entry.url).first()
),
realm: Realm.PreferDraft
)
})) as string | null
if (!url) return new Response('Not found', {status: 404})
const source = new URL(request.url)
Expand Down

0 comments on commit 121ff48

Please sign in to comment.