-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
optimise <title>
- Loading branch information
Showing
18 changed files
with
710 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { stringify } from '../../utils/stringify'; | ||
import getExpressionPrecedence from '../../utils/getExpressionPrecedence'; | ||
import Node from './shared/Node'; | ||
import Block from '../dom/Block'; | ||
|
||
export default class Title extends Node { | ||
build( | ||
block: Block, | ||
parentNode: string, | ||
parentNodes: string | ||
) { | ||
const isDynamic = !!this.children.find(node => node.type !== 'Text'); | ||
|
||
if (isDynamic) { | ||
let value; | ||
|
||
const allDependencies = new Set(); | ||
let shouldCache; | ||
|
||
// TODO some of this code is repeated in Tag.ts — would be good to | ||
// DRY it out if that's possible without introducing crazy indirection | ||
if (this.children.length === 1) { | ||
// single {{tag}} — may be a non-string | ||
const { expression } = this.children[0]; | ||
const { indexes } = block.contextualise(expression); | ||
const { dependencies, snippet } = this.children[0].metadata; | ||
|
||
value = snippet; | ||
dependencies.forEach(d => { | ||
allDependencies.add(d); | ||
}); | ||
|
||
shouldCache = ( | ||
expression.type !== 'Identifier' || | ||
block.contexts.has(expression.name) | ||
); | ||
} else { | ||
// '{{foo}} {{bar}}' — treat as string concatenation | ||
value = | ||
(this.children[0].type === 'Text' ? '' : `"" + `) + | ||
this.children | ||
.map((chunk: Node) => { | ||
if (chunk.type === 'Text') { | ||
return stringify(chunk.data); | ||
} else { | ||
const { indexes } = block.contextualise(chunk.expression); | ||
const { dependencies, snippet } = chunk.metadata; | ||
|
||
dependencies.forEach(d => { | ||
allDependencies.add(d); | ||
}); | ||
|
||
return getExpressionPrecedence(chunk.expression) <= 13 ? `(${snippet})` : snippet; | ||
} | ||
}) | ||
.join(' + '); | ||
|
||
shouldCache = true; | ||
} | ||
|
||
const last = shouldCache && block.getUniqueName( | ||
`title_value` | ||
); | ||
|
||
if (shouldCache) block.addVariable(last); | ||
|
||
let updater; | ||
const init = shouldCache ? `${last} = ${value}` : value; | ||
|
||
block.builders.init.addLine( | ||
`document.title = ${init};` | ||
); | ||
updater = `document.title = ${shouldCache ? last : value};`; | ||
|
||
if (allDependencies.size) { | ||
const dependencies = Array.from(allDependencies); | ||
const changedCheck = ( | ||
( block.hasOutroMethod ? `#outroing || ` : '' ) + | ||
dependencies.map(dependency => `changed.${dependency}`).join(' || ') | ||
); | ||
|
||
const updateCachedValue = `${last} !== (${last} = ${value})`; | ||
|
||
const condition = shouldCache ? | ||
( dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue ) : | ||
changedCheck; | ||
|
||
block.builders.update.addConditional( | ||
condition, | ||
updater | ||
); | ||
} | ||
} else { | ||
const value = stringify(this.children[0].data); | ||
block.builders.hydrate.addLine(`document.title = ${value};`); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { SsrGenerator } from '../index'; | ||
import Block from '../Block'; | ||
import { escape } from '../../../utils/stringify'; | ||
import visit from '../visit'; | ||
import { Node } from '../../../interfaces'; | ||
|
||
export default function visitTitle( | ||
generator: SsrGenerator, | ||
block: Block, | ||
node: Node | ||
) { | ||
generator.append(`<title>`); | ||
|
||
node.children.forEach((child: Node) => { | ||
visit(generator, block, child); | ||
}); | ||
|
||
generator.append(`</title>`); | ||
} |
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,8 +1,16 @@ | ||
import validateElement from './validateElement'; | ||
import { Validator } from '../index'; | ||
import { Node } from '../../interfaces'; | ||
|
||
export default function validateHead(validator: Validator, node: Node, refs: Map<string, Node[]>, refCallees: Node[]) { | ||
if (node.attributes.length) { | ||
validator.error(`<:Head> should not have any attributes or directives`, node.start); | ||
} | ||
|
||
// TODO ensure only valid elements are included here | ||
|
||
node.children.forEach(node => { | ||
if (node.type !== 'Element') return; // TODO handle {{#if}} and friends? | ||
validateElement(validator, node, refs, refCallees, [], []); | ||
}); | ||
} |
Oops, something went wrong.