pencilflip
02/01/2022, 12:26 AMpencilflip
02/01/2022, 12:26 AMCоlе
02/01/2022, 1:37 AMCоlе
02/01/2022, 1:45 AMts
export const onRequestGet: PagesFunction<{}> = async ({ request, env, next, data }) => {
// Get api data and page at same time
let apiData = fetch(`INSERT API URL/whatever`).then(res => res.json());
let pageData = await next();
let [api, page] = await Promise.all([apiData, pageData]);
return new HTMLRewriter().on("head", new HeadHandler(api, env)).transform(page);
};
class HeadHandler {
private api: any;
constructor(api: any) {
this.api = api;
}
comments(comment: Comment) {
if (comment.text.trim() !== "INJECT_META_TAGS") { return; }
// Append twitter meta tags
comment.after(`<meta property="twitter:card" content="summary">`, { html: true });
comment.after(`<meta property="twitter:title" content="${this.api.title}">`, { html: true });
comment.after(`<meta property="twitter:description" content="${this.api.description}">`, { html: true });
}
}
Then, I just put <!-- INJECT_META_TAGS -->
in the head of my page, and the HTMLRewriter will insert the tags after itCоlе
02/01/2022, 1:46 AMawait next()
with however you're getting the HTML for the page, if you're not just intercepting the request/using middleware.Cоlе
02/01/2022, 1:47 AMCоlе
02/01/2022, 2:11 AMindex.ts
, then calling next()
will return a Promise<Response>
for index.htmlzsmooth
02/01/2022, 2:12 AMpencilflip
02/01/2022, 2:25 AMpencilflip
02/01/2022, 2:26 AMpencilflip
02/01/2022, 2:26 AMyarn build
and then npx wrangler pages dev build
(for my CRA app), only the home page gets renderedpencilflip
02/01/2022, 2:26 AMlocalhost:3000/test
, it doesn't renderpencilflip
02/01/2022, 2:27 AMfunction Test() {
return <div>Test</div>;
}
zsmooth
02/01/2022, 2:34 AMfunctions/[[path]].js
is what i believe the file name needs to be to route everything to itpencilflip
02/01/2022, 2:40 AMCоlе
02/01/2022, 2:47 AMCоlе
02/01/2022, 2:47 AMCоlе
02/01/2022, 2:48 AM[[path]].ts
which would catch all pages, then try to get it to serve index.html
Cоlе
02/01/2022, 2:49 AM/index.html
but browser sees /whatever-path-you-went-to
Cоlе
02/01/2022, 2:50 AM[path].ts
to only catch 1-depth "routes", then 404 everything deeper, such was /whatever/path
bryan.
02/01/2022, 5:19 PMJames
02/01/2022, 5:24 PM@cloudflare/workers-types
exports a PagesFunction
type which can be used. Is there anything else you need?bryan.
02/01/2022, 5:44 PMHTMLRewriter
?bryan.
02/01/2022, 5:45 PM[path].ts
and [[path]].ts
at the time being? we'd ideally like to do some type of regex matching to filter this down to a smaller set of routes.bryan.
02/01/2022, 5:46 PMJames
02/01/2022, 5:46 PMJames
02/01/2022, 5:47 PMJames
02/01/2022, 5:49 PMJames
02/01/2022, 5:49 PMbryan.
02/01/2022, 5:52 PM