Tuesday, 19 April 2022

On Github Pages you can run a React application as a single file and call it as a function. What's the best way to do that on Google Cloud Platform?

I have a Github repo that has Pages enabled. In Pages, I have an index.js that contains a function. The function is run automatically when you hit an HTTP endpoint. The application is a widget, so it's not like I want to host a whole website. I jsut want to be able to hit an endpoint from a <script> tag and run my code in a third-party site.

The index.js file is created by building my React app with Parcel and compiling it down to a single js file. The file itself runs an IIFE - this is created by Parcel after building and looks something like this:

!function(){function e(e,t,n,r){Object.defineProperty...
// hundreds of lines minified into one humongus line of code
...{domElement:e})}),e)})),Er()}();

For reference, I created it by following this tutorial, and you can see the author's script in their github repo here.

But Github PAges is public, so I wanted to move my script to GCP. I thought a simple Cloud function would do, but I get the dreaded could not handle the request error. For my Cloud Functions app I tried the following:

exports.helloWorld = (req, res) => {
  const widget = // copy in all of the above code
  res.status(200).send(widget);
}

This threw the above error.

Is there a better/different way to host a single function script like this that runs on http call?



from On Github Pages you can run a React application as a single file and call it as a function. What's the best way to do that on Google Cloud Platform?

No comments:

Post a Comment