-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
286 additions
and
57 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const siteMap = `<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<url> | ||
<loc>https://crisp-react.winwiz1.com</loc> | ||
</url> | ||
<url> | ||
<loc>https://crisp-react.winwiz1.com/a</loc> | ||
</url> | ||
<url> | ||
<loc>https://crisp-react.winwiz1.com/namelookup</loc> | ||
</url> | ||
<url> | ||
<loc>https://crisp-react.winwiz1.com/lighthouse</loc> | ||
</url> | ||
<url> | ||
<loc>https://crisp-react.winwiz1.com/second</loc> | ||
</url> | ||
</urlset> | ||
`; | ||
|
||
const bots = [ | ||
"googlebot", | ||
"bingbot", | ||
"yahoo", | ||
"applebot", | ||
"yandex", | ||
"baidu", | ||
]; | ||
|
||
class ElementHandler { | ||
element(element) { | ||
element?.replace('<div id="app-root"></div>', {html: true}); | ||
} | ||
|
||
comments(comment) { | ||
} | ||
|
||
text(text) { | ||
} | ||
} | ||
|
||
addEventListener("fetch", event => { | ||
event.respondWith(handleRequest(event.request)) | ||
}) | ||
|
||
/** | ||
* Respond to the request | ||
* @param {Request} request | ||
*/ | ||
async function handleRequest(req) { | ||
const parsedUrl = new URL(req.url); | ||
const path = parsedUrl.pathname.toLowerCase(); | ||
const lastIdx = path.lastIndexOf("."); | ||
const extensionLess = lastIdx === -1; | ||
const extension = path.substring(lastIdx); | ||
const userAgent = (req.headers.get("User-Agent") || "")?.toLowerCase() ?? ""; | ||
|
||
|
||
if (path === "/sitemap.xml") { | ||
return new Response( | ||
siteMap, | ||
{ | ||
headers: {"content-type": "text/xml;charset=UTF-8"}, | ||
} | ||
); | ||
} | ||
if ((extension === ".html" || extensionLess === true) && | ||
bots.some(bot => userAgent.indexOf(bot) !== -1)) { | ||
const res = await fetch(req); | ||
return new HTMLRewriter().on( | ||
"div[id='app-root']", | ||
new ElementHandler() | ||
).transform(res); | ||
} else { | ||
return fetch(req); | ||
} | ||
} |
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,114 @@ | ||
const siteMap = `<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<url> | ||
<loc>https://jamstack.winwiz1.com</loc> | ||
</url> | ||
<url> | ||
<loc>https://jamstack.winwiz1.com/a</loc> | ||
</url> | ||
<url> | ||
<loc>https://jamstack.winwiz1.com/namelookup</loc> | ||
</url> | ||
<url> | ||
<loc>https://jamstack.winwiz1.com/lighthouse</loc> | ||
</url> | ||
<url> | ||
<loc>https://jamstack.winwiz1.com/second</loc> | ||
</url> | ||
</urlset> | ||
`; | ||
|
||
const robotsTxt = `User-agent: * | ||
Allow: / | ||
Sitemap: https://jamstack.winwiz1.com/sitemap.xml | ||
`; | ||
|
||
const pagesDomain = "https://crisp-react.pages.dev"; | ||
|
||
const bots = [ | ||
"googlebot", | ||
"bingbot", | ||
"yahoo", | ||
"applebot", | ||
"yandex", | ||
"baidu", | ||
]; | ||
|
||
class ElementHandler { | ||
element(element) { | ||
element?.replace('<div id="app-root"></div>', {html: true}); | ||
} | ||
|
||
comments(comment) { | ||
} | ||
|
||
text(text) { | ||
} | ||
} | ||
|
||
const internalSpaPages = [ | ||
"/a", | ||
"/namelookup", | ||
"/lighthouse" | ||
]; | ||
|
||
const landingSpaPage = "/first"; | ||
|
||
function redirectRequest(path) { | ||
if (path === "/" || internalSpaPages.includes(path)) { | ||
return landingSpaPage; | ||
} | ||
return path; | ||
} | ||
|
||
addEventListener("fetch", (event) => { | ||
event.respondWith( | ||
handleRequest(event.request).catch( | ||
(err) => new Response(err.stack, { status: 500 }) | ||
) | ||
); | ||
}); | ||
|
||
/** | ||
* Respond to the request | ||
* @param {Request} request | ||
*/ | ||
async function handleRequest(req) { | ||
const parsedUrl = new URL(req.url); | ||
const path = parsedUrl.pathname.toLowerCase(); | ||
const lastIdx = path.lastIndexOf("."); | ||
const extensionLess = lastIdx === -1; | ||
const extension = path.substring(lastIdx); | ||
const userAgent = (req.headers.get("User-Agent") || "")?.toLowerCase() ?? ""; | ||
const urlToFetch = pagesDomain + redirectRequest(path); | ||
|
||
if (path === "/sitemap.xml") { | ||
return new Response( | ||
siteMap, | ||
{ | ||
headers: {"content-type": "text/xml;charset=UTF-8"}, | ||
} | ||
); | ||
} | ||
|
||
if (path === "/robots.txt") { | ||
return new Response( | ||
robotsTxt, | ||
{ | ||
headers: {"content-type": "text/plain;charset=UTF-8"}, | ||
} | ||
); | ||
} | ||
|
||
if ((extension === ".html" || extensionLess === true) && | ||
bots.some(bot => userAgent.indexOf(bot) !== -1)) { | ||
const res = await fetch(urlToFetch, req); | ||
return new HTMLRewriter().on( | ||
"div[id='app-root']", | ||
new ElementHandler() | ||
).transform(res); | ||
} else { | ||
return fetch(urlToFetch, req); | ||
} | ||
} |
Oops, something went wrong.