Skip to content

Commit

Permalink
feat: add encryption/clear history API
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Oct 13, 2024
1 parent b3d1809 commit f9c6c01
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function defineConfig<T extends SharedData>(
versionCache,
rootView: config.rootView ?? 'inertia_layout',
sharedData: config.sharedData! || {},
history: { encrypt: config.history?.encrypt ?? false },
entrypoint: slash(
config.entrypoint ?? (await detector.detectEntrypoint('inertia/app/app.ts'))
),
Expand Down
28 changes: 27 additions & 1 deletion src/inertia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ export class Inertia {
#sharedData: SharedData = {}
#serverRenderer: ServerRenderer

#shouldClearHistory = false
#shouldEncryptHistory = false

constructor(
protected ctx: HttpContext,
protected config: ResolvedConfig,
protected vite?: Vite
) {
this.#sharedData = config.sharedData
this.#serverRenderer = new ServerRenderer(config, vite)

this.#shouldClearHistory = false
this.#shouldEncryptHistory = config.history.encrypt
}

/**
Expand Down Expand Up @@ -133,7 +139,7 @@ export class Inertia {
await Promise.all(
Object.entries(props).map(async ([key, value]) => {
if (typeof value === 'function') {
return [key, await value()]
return [key, await value(this.ctx)]
}

if (
Expand Down Expand Up @@ -209,6 +215,8 @@ export class Inertia {
url: this.ctx.request.url(true),
version: this.config.versionCache.getVersion(),
props: await this.#resolvePageProps(propsToResolve),
clearHistory: this.#shouldClearHistory,
encryptHistory: this.#shouldEncryptHistory,
...this.#resolveMergeProps(pageProps),
...this.#resolveDeferredProps(component, pageProps),
}
Expand Down Expand Up @@ -289,6 +297,24 @@ export class Inertia {
return pageObject
}

/**
* Clear history state.
*
* See https://v2.inertiajs.com/history-encryption#clearing-history
*/
clearHistory() {
this.#shouldClearHistory = true
}

/**
* Encrypt history
*
* See https://v2.inertiajs.com/history-encryption
*/
encryptHistory(encrypt = true) {
this.#shouldEncryptHistory = encrypt
}

/**
* Create a lazy prop
*
Expand Down
48 changes: 46 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ export interface InertiaConfig<T extends SharedData = SharedData> {
*/
sharedData?: T

/**
* History encryption
*
* See https://v2.inertiajs.com/history-encryption
*/
history?: {
encrypt?: boolean
}

/**
* Options to configure SSR
*/
Expand Down Expand Up @@ -89,6 +98,7 @@ export interface ResolvedConfig<T extends SharedData = SharedData> {
rootView: string | ((ctx: HttpContext) => string)
versionCache: VersionCache
sharedData: T
history: { encrypt: boolean }
ssr: {
enabled: boolean
entrypoint: string
Expand All @@ -98,14 +108,48 @@ export interface ResolvedConfig<T extends SharedData = SharedData> {
}

export interface PageObject<TPageProps extends PageProps = PageProps> {
ssrHead?: string
ssrBody?: string

/**
* The name of the JavaScript page component.
*/
component: string

/**
* The current asset version.
*/
version: string | number

/**
* The page props (data).
*/
props: TPageProps

/**
* The page URL.
*/
url: string
ssrHead?: string
ssrBody?: string

/**
* List of deferred props that will be loaded with subsequent requests
*/
deferredProps?: Record<string, string[]>

/**
* List of mergeable props that will be merged with subsequent requests
*/
mergeProps?: string[]

/**
* Whether or not to encrypt the current page's history state.
*/
encryptHistory?: boolean

/**
* Whether or not to clear any encrypted history state.
*/
clearHistory?: boolean
}

type IsOptionalProp<T> =
Expand Down
39 changes: 39 additions & 0 deletions tests/inertia.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ test.group('Inertia', () => {
version: '1',
props: { foo: 'bar' },
url: null,
clearHistory: false,
encryptHistory: false,
})
})

Expand Down Expand Up @@ -88,6 +90,8 @@ test.group('Inertia', () => {
version: '1',
props: { foo: 'bar' },
url: null,
encryptHistory: false,
clearHistory: false,
})
})

Expand Down Expand Up @@ -396,6 +400,41 @@ test.group('Inertia', () => {

assert.deepEqual(result.props, { baz: 'baz' })
})

test('encrypt history with config file', async ({ assert }) => {
const inertia = await new InertiaFactory()
.merge({ config: { history: { encrypt: true } } })
.withXInertiaHeader()
.create()

const result: any = await inertia.render('foo')

assert.isTrue(result.encryptHistory)
})

test('encrypt history with api', async ({ assert }) => {
const inertia = await new InertiaFactory()
.merge({ config: { history: { encrypt: false } } })
.withXInertiaHeader()
.create()

inertia.encryptHistory()
const result: any = await inertia.render('foo')

assert.isTrue(result.encryptHistory)
})

test('clear history with api', async ({ assert }) => {
const inertia = await new InertiaFactory()
.merge({ config: { history: { encrypt: false } } })
.withXInertiaHeader()
.create()

inertia.clearHistory()
const result: any = await inertia.render('foo')

assert.isTrue(result.clearHistory)
})
})

test.group('Inertia | Ssr', () => {
Expand Down
7 changes: 7 additions & 0 deletions tests/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ test.group('Middleware', () => {
sharedData: {},
versionCache: new VersionCache(new URL(import.meta.url), '1'),
ssr: { enabled: false, bundle: '', entrypoint: '' },
history: { encrypt: false },
})

await middleware.handle(ctx, () => {})
Expand All @@ -48,6 +49,7 @@ test.group('Middleware', () => {
sharedData: {},
versionCache: new VersionCache(new URL(import.meta.url), '1'),
ssr: { enabled: false, bundle: '', entrypoint: '' },
history: { encrypt: false },
})

await middleware.handle(ctx, () => {
Expand Down Expand Up @@ -77,6 +79,7 @@ test.group('Middleware', () => {
sharedData: {},
versionCache: new VersionCache(new URL(import.meta.url), '1'),
ssr: { enabled: false, bundle: '', entrypoint: '' },
history: { encrypt: false },
})

await middleware.handle(ctx, () => {
Expand Down Expand Up @@ -106,6 +109,7 @@ test.group('Middleware', () => {
sharedData: {},
versionCache: new VersionCache(new URL(import.meta.url), '1'),
ssr: { enabled: false, bundle: '', entrypoint: '' },
history: { encrypt: false },
})

await middleware.handle(ctx, () => {
Expand Down Expand Up @@ -133,6 +137,7 @@ test.group('Middleware', () => {
sharedData: {},
versionCache: new VersionCache(new URL(import.meta.url), '1'),
ssr: { enabled: false, bundle: '', entrypoint: '' },
history: { encrypt: false },
})

await middleware.handle(ctx, () => {})
Expand All @@ -154,6 +159,7 @@ test.group('Middleware', () => {
sharedData: {},
versionCache: version,
ssr: { enabled: false, bundle: '', entrypoint: '' },
history: { encrypt: false },
})

const server = httpServer.create(async (req, res) => {
Expand Down Expand Up @@ -185,6 +191,7 @@ test.group('Middleware', () => {
sharedData: {},
versionCache: version,
ssr: { enabled: false, bundle: '', entrypoint: '' },
history: { encrypt: false },
})

const server = httpServer.create(async (req, res) => {
Expand Down

0 comments on commit f9c6c01

Please sign in to comment.