Skip to content

Commit

Permalink
Merge pull request #871 from adobecom/MWPW-152107
Browse files Browse the repository at this point in the history
MWPW-152107 [EdgeWorker] Handle pages with unity workflow
  • Loading branch information
Blainegunn authored Oct 29, 2024
2 parents f513062 + 2655b15 commit c5eab8d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 14 additions & 11 deletions edgeworkers/Acrobat_DC_web_prod/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ export async function responseProvider(request) {

// Make preliminary pass through the content to capture version metadata
const firstPassRewriter = new HtmlRewritingStream();
let version, widgetVersion, mobileWidget;
let version, widgetVersion, mobileWidget, unityWorkflow;
const prefix = isProd ? '' : 'stg-';
firstPassRewriter.onElement(`meta[name="${prefix}dc-widget-version"]`, el => {
widgetVersion = el.getAttribute('content');
});
firstPassRewriter.onElement(`meta[name="${prefix}dc-generate-cache-version"]`, el => {
version = el.getAttribute('content');
});
firstPassRewriter.onElement(`meta[name="mobile-widget"]`, el => {
firstPassRewriter.onElement('meta[name="mobile-widget"]', el => {
mobileWidget = el.getAttribute('content');
});
firstPassRewriter.onElement('.unity.workflow-acrobat', el => {
unityWorkflow = true;
});
const nullWriter = new WritableStream({
write() {},
close() {},
Expand Down Expand Up @@ -76,7 +79,7 @@ export async function responseProvider(request) {
delete responseHeaders[prop];
}

return [responseStream, responseHeaders, version, widgetVersion, mobileWidget];
return [responseStream, responseHeaders, version, widgetVersion, mobileWidget, unityWorkflow];
};

const fetchResource = async path => {
Expand All @@ -88,13 +91,13 @@ export async function responseProvider(request) {
};

const fetchFrictionlessPageAndInlineSnippet = async () => {
const [responseStream, responseHeaders, version, widgetVersion, mobileWidget] = await fetchFrictionlessPage();
const [responseStream, responseHeaders, version, widgetVersion, mobileWidget, unityWorkflow] = await fetchFrictionlessPage();

if (!verb || !locale || !version || !widgetVersion) {
throw new Error('Missing metadata');
}

if (!(mobileWidget && request.device.isMobile)) {
if (!(mobileWidget && request.device.isMobile) && !unityWorkflow) {
const snippet =
await fetchResource(`/dc/dc-generate-cache/dc-hosted-${version}/${verb}-${locale}.html`);
const snippetHead = snippet.substring(snippet.indexOf('<head>')+6, snippet.indexOf('</head>'));
Expand All @@ -109,19 +112,19 @@ export async function responseProvider(request) {
}
const dcCoreVersion = widgetVersion.split("_")[0];

return [responseStream, responseHeaders, dcCoreVersion, mobileWidget];
return [responseStream, responseHeaders, dcCoreVersion, mobileWidget, unityWorkflow];
};

const scriptHashes = [];

const inlineScripts = async (mobileWidget, scripts, dcConverter) => {
const inlineScripts = async (unityWorkflow, mobileWidget, scripts, dcConverter) => {
// Inline dc-converter-widget.js and scripts.js. Remove modular definition and import.
// Change relative paths to absolute. Remove JS-driven CSP in favor of HTTP header.
let inlineScript = scripts
.replace('await import(\'./contentSecurityPolicy/csp.js\')', '{default:()=>{}}')
.replace('await import(\'./dcLana.js\')', 'await import(\'/acrobat/scripts/dcLana.js\')');

if (!(mobileWidget && request.device.isMobile)) {
if (!(mobileWidget && request.device.isMobile) && !unityWorkflow) {
inlineScript = dcConverter
.replace('export default', 'const dcConverter = ')
.replace('import(\'../../scripts/frictionless.js\')', 'import(\'/acrobat/scripts/frictionless.js\')')
Expand Down Expand Up @@ -154,7 +157,7 @@ export async function responseProvider(request) {

try {
const [
[responseStream, responseHeaders, dcCoreVersion, mobileWidget],
[responseStream, responseHeaders, dcCoreVersion, mobileWidget, unityWorkflow],
scripts,
dcConverter,
dcStyles,
Expand All @@ -167,7 +170,7 @@ export async function responseProvider(request) {
fetchResource('/libs/styles/styles.css'),
]);

await inlineScripts(mobileWidget, scripts, dcConverter);
await inlineScripts(unityWorkflow, mobileWidget, scripts, dcConverter);
inlineStyles(dcStyles, miloStyles);

const csp = contentSecurityPolicy(isProd, scriptHashes);
Expand All @@ -181,7 +184,7 @@ export async function responseProvider(request) {
'<https://use.typekit.net>;rel="preconnect"',
`</libs/deps/imslib.min.js>;rel="preload";as="script"`,
];
if (!(mobileWidget && request.device.isMobile)) {
if (!(mobileWidget && request.device.isMobile) && !unityWorkflow) {
headerLink = [...headerLink,
`<${acrobat}>;rel="preconnect"`,
`<${pdfnow}>;rel="preconnect"`,
Expand Down
4 changes: 3 additions & 1 deletion test/edgeworkers/__mocks__/html-rewriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export const mockOnElement = jest.fn().mockImplementation((elem, fn) => {
append: jest.fn(),
replaceWith: jest.fn(),
};
fn(el);
if (elem !== '.unity.workflow-acrobat') {
fn(el);
}
return {
selector: String,
handler: Element()
Expand Down

0 comments on commit c5eab8d

Please sign in to comment.