Skip to content

Commit

Permalink
Prerender code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
amits97 committed Oct 29, 2023
1 parent 8fda15e commit e59f7d7
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,6 @@ function trimUrl(targetUrl) {
return cleanUrl.replace(/\/$/, "");
}

function urlWithPreservedTabParam(targetUrl) {
let cleanUrl = trimUrl(targetUrl);

const allowedTabNames = ["chords", "tabs", "video"];
let rawUrl = new URL(targetUrl);
let rawTabParam = new URLSearchParams(rawUrl.search).get("tab");
let whitelistedTabName =
allowedTabNames[allowedTabNames.indexOf(rawTabParam)];

if (whitelistedTabName) {
let cleanUrlObj = new URL(cleanUrl);
cleanUrlObj.searchParams.append("tab", whitelistedTabName);
cleanUrl = cleanUrlObj.href;
}

return cleanUrl;
}

export async function handler(event) {
const ERROR_MESSAGE = "No query parameter given!";
const INVALID_URL = "Invalid URL!";
Expand All @@ -87,10 +69,12 @@ export async function handler(event) {
"www.nadanchords.com",
"nadanchords.com",
];
const ALLOWED_TAB_NAMES = ["chords", "tabs", "video"];
const HOSTNAME = "https://www.naadanchords.com";

if (event.queryStringParameters) {
const targetUrl = trimUrl(event.queryStringParameters.url);
const rawUrl = event.queryStringParameters.url;
const targetUrl = trimUrl(rawUrl);
let parsedUrl;

if (!targetUrl) {
Expand All @@ -102,10 +86,17 @@ export async function handler(event) {
}
}

let rawUrlObj = new URL(rawUrl);
let rawTabParam = new URLSearchParams(rawUrlObj.search).get("tab");
let whitelistedTabName =
ALLOWED_TAB_NAMES[ALLOWED_TAB_NAMES.indexOf(rawTabParam)];

if (whitelistedTabName) {
parsedUrl.searchParams.append("tab", whitelistedTabName);
}

//Check if cache present in DynamoDB
let cache = await dynamoDbCache(
urlWithPreservedTabParam(event.queryStringParameters.url)
);
let cache = await dynamoDbCache(parsedUrl.href);
if (cache) {
return success(cache);
}
Expand Down Expand Up @@ -140,10 +131,14 @@ export async function handler(event) {
try {
let page = await browser.newPage();

let url = new URL(whitelistedURL);

if (whitelistedTabName) {
// Set tab name
url.searchParams.append("tab", whitelistedTabName);
}

// Set flag to disable speedy mode
let url = new URL(
urlWithPreservedTabParam(event.queryStringParameters.url)
);
url.searchParams.append("isPrerendered", true);
await page.goto(url.href);

Expand Down Expand Up @@ -190,10 +185,8 @@ export async function handler(event) {
}
}
browser.close();
await writeDynamoDbCache(
urlWithPreservedTabParam(event.queryStringParameters.url),
result
);
url.searchParams.delete("isPrerendered");
await writeDynamoDbCache(url.href, result);
return success(result);
} catch (e) {
return failure(e);
Expand Down

0 comments on commit e59f7d7

Please sign in to comment.