Skip to content

Commit

Permalink
Fix title when not used by default (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodekker authored and reinink committed Sep 9, 2022
1 parent 958181d commit f93963d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/inertia-react/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function App({
return createHeadManager(
typeof window === 'undefined',
titleCallback || (title => title),
onHeadUpdate || (() => {})
onHeadUpdate || (() => {}),
)
}, [])

Expand All @@ -37,6 +37,8 @@ export default function App({
}))
},
})

Inertia.on('navigate', () => headManager.forceUpdate())
}, [])

if (!current.component) {
Expand Down
2 changes: 2 additions & 0 deletions packages/inertia-vue/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default {
this.key = preserveState ? this.key : Date.now()
},
})

Inertia.on('navigate', () => headManager.forceUpdate())
}
},
render(h) {
Expand Down
2 changes: 2 additions & 0 deletions packages/inertia-vue3/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default {
key.value = args.preserveState ? key.value : Date.now()
},
})

Inertia.on('navigate', () => headManager.forceUpdate())
}

return () => {
Expand Down
16 changes: 14 additions & 2 deletions packages/inertia/src/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ const Renderer = {
}

export default function createHeadManager(isServer: boolean, titleCallback: ((title: string) => string), onUpdate: ((elements: string[]) => void)): ({
forceUpdate: () => void,
createProvider: () => ({
update: (elements: Array<string>) => void,
update: (elements: string[]) => void,
disconnect: () => void,
})
}) {
Expand Down Expand Up @@ -86,6 +87,12 @@ export default function createHeadManager(isServer: boolean, titleCallback: ((ti
}

function collect(): Array<string> {
const title = titleCallback('')

const defaults: Record<string, string> = {
... (title ? { title: `<title inertia="">${title}</title>`} : {}),
}

const elements = Object.values(states)
.reduce((carry, elements) => carry.concat(elements), [])
.reduce((carry, element) => {
Expand All @@ -107,7 +114,7 @@ export default function createHeadManager(isServer: boolean, titleCallback: ((ti
}

return carry
}, {} as Record<string, string>)
}, defaults)

return Object.values(elements)
}
Expand All @@ -116,7 +123,12 @@ export default function createHeadManager(isServer: boolean, titleCallback: ((ti
isServer ? onUpdate(collect()) : Renderer.update(collect())
}

// By committing during initialization, we can guarantee that the default
// tags are set, as well as that they exist during SSR itself.
commit()

return {
forceUpdate: commit,
createProvider: function () {
const id = connect()

Expand Down

0 comments on commit f93963d

Please sign in to comment.