-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: vanilla function resolves Fixes #435 * chore: broken test * perf: isolate plugin logic
- Loading branch information
Showing
17 changed files
with
133 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './createHead' | ||
export * from './debounced' | ||
export * from './domPlugin' | ||
export * from './plugins/domPlugin' | ||
export * from './renderDOMHead' |
4 changes: 2 additions & 2 deletions
4
packages/unhead/src/client/domPlugin.ts → ...es/unhead/src/client/plugins/domPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import type { CreateHeadOptions, Head } from '@unhead/schema' | ||
import { createHeadCore } from '../createHead' | ||
import PayloadPlugin from './plugins/payload' | ||
import { ServerEventHandlerPlugin } from './plugins/eventHandlers' | ||
import { PayloadPlugin } from './plugins/payload' | ||
|
||
export function createHead<T extends Record<string, any> = Head>(options: CreateHeadOptions = {}) { | ||
// @ts-expect-error untyped | ||
const head = createHeadCore<T>({ ...options, document: false }) | ||
head.use(PayloadPlugin) | ||
return head | ||
return createHeadCore<T>({ | ||
...options, | ||
// @ts-expect-error untyped | ||
document: false, | ||
plugins: [ | ||
...(options.plugins || []), | ||
PayloadPlugin, | ||
ServerEventHandlerPlugin, | ||
], | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { defineHeadPlugin, hashCode, NetworkEvents } from '@unhead/shared' | ||
|
||
const ValidEventTags = new Set(['script', 'link', 'bodyAttrs']) | ||
|
||
/** | ||
* Supports DOM event handlers (i.e `onload`) as functions. | ||
* | ||
* When SSR we need to strip out these values. On CSR we | ||
*/ | ||
export const ServerEventHandlerPlugin = defineHeadPlugin({ | ||
hooks: { | ||
'tags:resolve': (ctx) => { | ||
for (const tag of ctx.tags) { | ||
if (!ValidEventTags.has(tag.tag)) { | ||
continue | ||
} | ||
|
||
const props = tag.props | ||
|
||
let hasEventHandlers = false | ||
for (const key in props) { | ||
// on | ||
if (key[0] !== 'o' || key[1] !== 'n') { | ||
continue | ||
} | ||
|
||
if (!Object.prototype.hasOwnProperty.call(props, key)) { | ||
continue | ||
} | ||
|
||
const value = props[key] | ||
|
||
if (typeof value !== 'function') { | ||
continue | ||
} | ||
|
||
// insert a inline script to set the status of onload and onerror | ||
if (NetworkEvents.has(key)) { | ||
props[key] = `this.dataset.${key}fired = true` | ||
hasEventHandlers = true | ||
} | ||
} | ||
|
||
if (hasEventHandlers && (tag.props.src || tag.props.href)) { | ||
tag.key = tag.key || hashCode(tag.props.src || tag.props.href) | ||
} | ||
} | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.