Skip to content

Commit 0d3d2f4

Browse files
justinphstaylor
authored andcommitted
fix: apply scotts changes
1 parent 5b689b0 commit 0d3d2f4

File tree

2 files changed

+96
-109
lines changed

2 files changed

+96
-109
lines changed

src/server.js

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -131,62 +131,55 @@ const getMethodsForTag = (type, tags, encode) => {
131131
}
132132
};
133133

134-
const mapStateOnServer = ({
135-
baseTag,
136-
bodyAttributes,
137-
encode,
138-
htmlAttributes,
139-
linkTags,
140-
metaTags,
141-
noscriptTags,
142-
scriptTags,
143-
styleTags,
144-
title = '',
145-
titleAttributes,
146-
prioritizeSeoTags,
147-
}) => {
148-
// these methods will be noops if prioritizeSeoTags is not true
134+
const getPriorityMethods = ({ metaTags, linkTags, scriptTags, encode }) => {
135+
const meta = prioritizer(metaTags, SEO_PRIORITY_TAGS.meta);
136+
const link = prioritizer(linkTags, SEO_PRIORITY_TAGS.link);
137+
const script = prioritizer(scriptTags, SEO_PRIORITY_TAGS.script);
138+
139+
// need to have toComponent() and toString()
140+
const priorityMethods = {
141+
toComponent: () => [
142+
...generateTagsAsReactComponent(TAG_NAMES.META, meta.priority),
143+
...generateTagsAsReactComponent(TAG_NAMES.LINK, link.priority),
144+
...generateTagsAsReactComponent(TAG_NAMES.SCRIPT, script.priority),
145+
],
146+
toString: () =>
147+
// generate all the tags as strings and concatenate them
148+
`${getMethodsForTag(TAG_NAMES.META, meta.priority, encode)} ${getMethodsForTag(
149+
TAG_NAMES.LINK,
150+
link.priority,
151+
encode
152+
)} ${getMethodsForTag(TAG_NAMES.SCRIPT, script.priority, encode)}`,
153+
};
154+
155+
return {
156+
priorityMethods,
157+
metaTags: meta.default,
158+
linkTags: link.default,
159+
scriptTags: script.default,
160+
};
161+
};
162+
163+
const mapStateOnServer = props => {
164+
const {
165+
baseTag,
166+
bodyAttributes,
167+
encode,
168+
htmlAttributes,
169+
noscriptTags,
170+
styleTags,
171+
title = '',
172+
titleAttributes,
173+
prioritizeSeoTags,
174+
} = props;
175+
let { linkTags, metaTags, scriptTags } = props;
149176
let priorityMethods = {
150177
toComponent: () => {},
151178
toString: () => {},
152179
};
153180
if (prioritizeSeoTags) {
154-
const meta = prioritizer(metaTags, SEO_PRIORITY_TAGS.meta);
155-
const link = prioritizer(linkTags, SEO_PRIORITY_TAGS.link);
156-
const script = prioritizer(scriptTags, SEO_PRIORITY_TAGS.script);
157-
158-
// need to have toComponent() and toString()
159-
priorityMethods = {
160-
toComponent: () => {
161-
const components = [];
162-
Array.prototype.push.apply(
163-
components,
164-
generateTagsAsReactComponent(TAG_NAMES.META, meta.priority)
165-
);
166-
Array.prototype.push.apply(
167-
components,
168-
generateTagsAsReactComponent(TAG_NAMES.LINK, link.priority)
169-
);
170-
Array.prototype.push.apply(
171-
components,
172-
generateTagsAsReactComponent(TAG_NAMES.SCRIPT, script.priority)
173-
);
174-
return components;
175-
},
176-
toString: () =>
177-
// generate all the tags as strings and concatenate them
178-
`${getMethodsForTag(TAG_NAMES.META, meta.priority, encode)} ${getMethodsForTag(
179-
TAG_NAMES.LINK,
180-
link.priority,
181-
encode
182-
)} ${getMethodsForTag(TAG_NAMES.SCRIPT, script.priority, encode)}`,
183-
};
184-
185-
metaTags = meta.default;
186-
linkTags = link.default;
187-
scriptTags = script.default;
181+
({ priorityMethods, linkTags, metaTags, scriptTags } = getPriorityMethods(props));
188182
}
189-
190183
return {
191184
priority: priorityMethods,
192185
base: getMethodsForTag(TAG_NAMES.BASE, baseTag, encode),

src/utils.js

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ const getInnermostProperty = (propsList, property) => {
2121
return null;
2222
};
2323

24-
const getAnyTrueFromPropsList = (propsList, checkedTag) =>
25-
propsList.reduce((accumulator, currentPropList) =>
26-
currentPropList[checkedTag] ? true : accumulator
27-
);
28-
2924
const getTitleFromPropsList = propsList => {
3025
let innermostTitle = getInnermostProperty(propsList, TAG_NAMES.TITLE);
3126
const innermostTemplate = getInnermostProperty(propsList, HELMET_PROPS.TITLE_TEMPLATE);
@@ -176,23 +171,64 @@ const getTagsFromPropsList = (tagName, primaryAttributes, propsList) => {
176171
.reverse();
177172
};
178173

179-
// helper to inspect for matching props on components
180-
export const checkIfPropsMatch = (props, toMatch) => {
181-
const pairs = Object.entries(props);
182-
for (let i = 0; i < pairs.length; i += 1) {
183-
const [propName, propVal] = pairs[i];
184-
if (toMatch[propName]) {
185-
// e.g. if rel exists in the list of allowed props
186-
const propWeAreCheckingOut = toMatch[propName]; // e.g. [amphtml, alternate, etc]
187-
if (propWeAreCheckingOut.includes(propVal)) {
188-
return true;
189-
}
174+
const getAnyTrueFromPropsList = (propsList, checkedTag) =>
175+
propsList.reduce((accumulator, currentPropList) =>
176+
currentPropList[checkedTag] ? true : accumulator
177+
);
178+
179+
const reducePropsToState = propsList => ({
180+
baseTag: getBaseTagFromPropsList([TAG_PROPERTIES.HREF], propsList),
181+
bodyAttributes: getAttributesFromPropsList(ATTRIBUTE_NAMES.BODY, propsList),
182+
defer: getInnermostProperty(propsList, HELMET_PROPS.DEFER),
183+
encode: getInnermostProperty(propsList, HELMET_PROPS.ENCODE_SPECIAL_CHARACTERS),
184+
htmlAttributes: getAttributesFromPropsList(ATTRIBUTE_NAMES.HTML, propsList),
185+
linkTags: getTagsFromPropsList(
186+
TAG_NAMES.LINK,
187+
[TAG_PROPERTIES.REL, TAG_PROPERTIES.HREF],
188+
propsList
189+
),
190+
metaTags: getTagsFromPropsList(
191+
TAG_NAMES.META,
192+
[
193+
TAG_PROPERTIES.NAME,
194+
TAG_PROPERTIES.CHARSET,
195+
TAG_PROPERTIES.HTTPEQUIV,
196+
TAG_PROPERTIES.PROPERTY,
197+
TAG_PROPERTIES.ITEM_PROP,
198+
],
199+
propsList
200+
),
201+
noscriptTags: getTagsFromPropsList(TAG_NAMES.NOSCRIPT, [TAG_PROPERTIES.INNER_HTML], propsList),
202+
onChangeClientState: getOnChangeClientState(propsList),
203+
scriptTags: getTagsFromPropsList(
204+
TAG_NAMES.SCRIPT,
205+
[TAG_PROPERTIES.SRC, TAG_PROPERTIES.INNER_HTML],
206+
propsList
207+
),
208+
styleTags: getTagsFromPropsList(TAG_NAMES.STYLE, [TAG_PROPERTIES.CSS_TEXT], propsList),
209+
title: getTitleFromPropsList(propsList),
210+
titleAttributes: getAttributesFromPropsList(ATTRIBUTE_NAMES.TITLE, propsList),
211+
prioritizeSeoTags:
212+
propsList.prioritizeSeoTags &&
213+
getAnyTrueFromPropsList(propsList, HELMET_PROPS.PRIORITIZE_SEO_TAGS),
214+
});
215+
216+
export const flattenArray = possibleArray =>
217+
Array.isArray(possibleArray) ? possibleArray.join('') : possibleArray;
218+
219+
export { reducePropsToState };
220+
221+
const checkIfPropsMatch = (props, toMatch) => {
222+
const keys = Object.keys(props);
223+
for (let i = 0; i < keys.length; i += 1) {
224+
// e.g. if rel exists in the list of allowed props [amphtml, alternate, etc]
225+
if (toMatch[keys[i]] && toMatch[keys[i]].includes(props[keys[i]])) {
226+
return true;
190227
}
191228
}
192229
return false;
193230
};
194231

195-
// re-usable fn to prioritize tags by matching props
196232
export const prioritizer = (elementsList, propsToMatch) => {
197233
if (Array.isArray(elementsList)) {
198234
return elementsList.reduce(
@@ -209,45 +245,3 @@ export const prioritizer = (elementsList, propsToMatch) => {
209245
}
210246
return { default: elementsList };
211247
};
212-
213-
const reducePropsToState = propsList => {
214-
return {
215-
baseTag: getBaseTagFromPropsList([TAG_PROPERTIES.HREF], propsList),
216-
bodyAttributes: getAttributesFromPropsList(ATTRIBUTE_NAMES.BODY, propsList),
217-
defer: getInnermostProperty(propsList, HELMET_PROPS.DEFER),
218-
encode: getInnermostProperty(propsList, HELMET_PROPS.ENCODE_SPECIAL_CHARACTERS),
219-
htmlAttributes: getAttributesFromPropsList(ATTRIBUTE_NAMES.HTML, propsList),
220-
linkTags: getTagsFromPropsList(
221-
TAG_NAMES.LINK,
222-
[TAG_PROPERTIES.REL, TAG_PROPERTIES.HREF],
223-
propsList
224-
),
225-
metaTags: getTagsFromPropsList(
226-
TAG_NAMES.META,
227-
[
228-
TAG_PROPERTIES.NAME,
229-
TAG_PROPERTIES.CHARSET,
230-
TAG_PROPERTIES.HTTPEQUIV,
231-
TAG_PROPERTIES.PROPERTY,
232-
TAG_PROPERTIES.ITEM_PROP,
233-
],
234-
propsList
235-
),
236-
noscriptTags: getTagsFromPropsList(TAG_NAMES.NOSCRIPT, [TAG_PROPERTIES.INNER_HTML], propsList),
237-
onChangeClientState: getOnChangeClientState(propsList),
238-
scriptTags: getTagsFromPropsList(
239-
TAG_NAMES.SCRIPT,
240-
[TAG_PROPERTIES.SRC, TAG_PROPERTIES.INNER_HTML],
241-
propsList
242-
),
243-
styleTags: getTagsFromPropsList(TAG_NAMES.STYLE, [TAG_PROPERTIES.CSS_TEXT], propsList),
244-
title: getTitleFromPropsList(propsList),
245-
titleAttributes: getAttributesFromPropsList(ATTRIBUTE_NAMES.TITLE, propsList),
246-
prioritizeSeoTags: getAnyTrueFromPropsList(propsList, HELMET_PROPS.PRIORITIZE_SEO_TAGS),
247-
};
248-
};
249-
250-
export const flattenArray = possibleArray =>
251-
Array.isArray(possibleArray) ? possibleArray.join('') : possibleArray;
252-
253-
export { reducePropsToState };

0 commit comments

Comments
 (0)