Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
fix(renderPartial): respect disabledStyles for partial render (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaka5h committed Jun 6, 2022
1 parent 91115b3 commit 8209bbe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
17 changes: 17 additions & 0 deletions __tests__/server/middleware/sendHtml.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,23 @@ describe('sendHtml', () => {
expect(res.send.mock.calls[0][0]).toBe(`<!doctype html><html><head><title>Some Title</title><style class="ssr-css">.class { background: red; }</style></head>${appHtml}</html>`);
expect(removeInitialState(res.send.mock.calls[0][0])).not.toContain('undefined');
});

it('sends an incomplete HTML document without styles', () => {
req.appHtml = `<dangerously-return-only-doctype><!doctype html><html><head><title>Some Title</title></head>${appHtml}</html></dangerously-return-only-doctype>`;
req.store.getState.mockImplementationOnce(() => fromJS({
holocron: fromJS({
loaded: ['a'],
}),
intl: fromJS({ activeLocale: 'en-US' }),
rendering: fromJS({
renderPartialOnly: true,
disableStyles: true,
}),
}));
sendHtml(req, res);
expect(res.send).toHaveBeenCalledTimes(1);
expect(res.send.mock.calls[0][0]).toBe(`<!doctype html><html><head><title>Some Title</title></head>${appHtml}</html>`);
});
});

describe('renderTextOnly', () => {
Expand Down
17 changes: 10 additions & 7 deletions src/server/middleware/sendHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export function getBody({
export function renderPartial({
html: initialHtml,
store,
disableStyles,
}) {
let html = initialHtml;
// The partial route is commonly used for HTML partials, however email rendering requires
Expand All @@ -301,14 +302,16 @@ export function renderPartial({
.replace(/^<dangerously-return-only-doctype>/, '')
.replace(/<\/dangerously-return-only-doctype>$/, '');
}
if (!disableStyles) {
const styles = renderModuleStyles(store);

const styles = renderModuleStyles(store);

if (html.startsWith('<!doctype html>')) {
return html.replace('</head>', `${styles}</head>`);
if (html.startsWith('<!doctype html>')) {
html = html.replace('</head>', `${styles}</head>`);
} else {
html = styles + html;
}
}

return styles + html;
return html;
}

// TODO add additional client side scripts
Expand Down Expand Up @@ -342,7 +345,7 @@ export default function sendHtml(req, res) {
const allowedHtmlTags = clientInitialState.getIn(['rendering', 'renderTextOnlyOptions', 'allowedHtmlTags']);

if (renderPartialOnly) {
return safeSend(res, renderPartial({ html: req.appHtml, store }));
return safeSend(res, renderPartial({ html: req.appHtml, store, disableStyles }));
}

if (renderTextOnly) {
Expand Down

0 comments on commit 8209bbe

Please sign in to comment.