Skye
03/22/2023, 7:11 PMrajatfermat
03/22/2023, 7:12 PMindex.html
that hosts the react app.
My plan is to use functions/index.js
and write this html response dynamically. This will allow me to pump more __initial__ HTML. The react app would then hydrate from existing html on the page instead of creating the HTML. This shall lead to perf improvements.
Wrt to this, I have the following qs:
- How can I make requests from functions/index.js
to my backend api?
- functions/[index.js]
needs to write the <script/>
tag in the html response HEAD
. For that, I need to copy the output of webpack
when I run build (webpack fingerprints my static js assets) and put that in the HTML response inside my functions/index.js
. Does functions/index.js
have a FileReader
to scan the static assets in the project?GeorgeTailor
03/22/2023, 7:52 PMfetch
- functions don't have access to file system, you can, however, import HTML into your function, look here https://developers.cloudflare.com/pages/platform/functions/module-support/, note that you can only import HTML, bin, wasm or JS/TS. Probably also json but I haven't tried that. To rewrite your head element in you would use HTMLRewriter https://developers.cloudflare.com/workers/runtime-apis/html-rewriter#selectorsGeorgeTailor
03/22/2023, 8:00 PMon
N number of times.
I would expect the next on
handler to catch it and wire the logic to my handler, but it does not. Does HTMLRewriter not include dynamically injected content by previous handlers?
I also tried to just manipulate the HTML in the working handler but I get
[mf:err] GET /: RuntimeError: memory access out of bounds
when I try to manipulate freshly injected content, for example from this code:
element(element) {
this.results.forEach((result, i) => {
const newDetails = element.after(descriptionTemplate, { html: true });
newDetails.setAttribute('id', result.id);
})
}
timesking
03/22/2023, 8:25 PMKeizer
03/22/2023, 8:31 PMChaika
03/22/2023, 8:34 PMenv
on the context, if you named it ctx
, it should be ctx.env.MY_BUCKET
Chaika
03/22/2023, 8:35 PMKeizer
03/22/2023, 8:42 PMKeizer
03/22/2023, 9:30 PMtype Bindings = {
not expecting BindingsKeizer
03/22/2023, 9:31 PMrajatfermat
03/22/2023, 9:33 PMfunctions/index.js
import html from "../dist/index.html";
export function onRequestGet(context) {
console.log("Request received", context.params.index);
var newHtml = new HTMLRewriter()
.on('title', {
element: (element) => {
element.setInnerContent('New Title!');
},
})
.transform(html);
return new Response(newHtml, {
headers: { "content-type": "text/html" },
});
}
but I am getting the following error:
[mf:err] GET /8592f5ff-305b-4587-bdc7-985689f17922: TypeError: Cannot read properties of undefined (reading 'pipeTo') at HTMLRewriter.transform (/Users/rajatmittal/.nvm/versions/node/v16.18.0/lib/node_modules/wrangler/node_modules/@miniflare/html-rewriter/src/rewriter.ts:75:26) at onRequestGet
Keizer
03/22/2023, 9:34 PMKeizer
03/22/2023, 9:34 PMkian
03/22/2023, 9:34 PMKeizer
03/22/2023, 9:35 PMkian
03/22/2023, 9:35 PMKeizer
03/22/2023, 9:36 PMkian
03/22/2023, 9:36 PMtype
would disappear when transpiled to JS, it serves no purpose and isn't a keyword in JSkian
03/22/2023, 9:36 PMGeorgeTailor
03/22/2023, 9:36 PMimport html from "../dist/index.html";
export function onRequestGet(context) {
console.log("Request received", context.params.index);
const res = new Response(html, {
headers: { "content-type": "text/html" },
});
return new HTMLRewriter()
.on('title', {
element: (element) => {
element.setInnerContent('New Title!');
},
})
.transform(res);
}
rajatfermat
03/22/2023, 9:38 PMGeorgeTailor
03/22/2023, 9:39 PMKeizer
03/22/2023, 9:40 PMkian
03/22/2023, 9:41 PMenv
for auto-complete and typecheckingkian
03/22/2023, 9:41 PMKeizer
03/22/2023, 9:44 PMapp.get("/", async (c, next) => {
var vdata = await getTpls(c.env);
console.log('ENV: '+c.env);
console.log('VDATA: '+vdata);
return c.text(Mustache.render(vdata.index, vdata, vdata));
});
kian
03/22/2023, 9:45 PMKeizer
03/22/2023, 9:45 PMKeizer
03/22/2023, 9:46 PM