HTML & Javascript project: Manual trigger of GitHub Pages deploy; migrating project to npm, parcel bundler, gh-pages
gh-pages
Branch (Traditional Method):This is a common convention for GitHub Pages. You can set your GitHub Pages source to be a specific branch named
gh-pages
.Keep your main code on
main
: Your main project code (HTML, JS, etc.) stays on yourmain
branch.Deployable content on
gh-pages
: You only put the deployable version of your site (the HTML, JS, CSS, etc., that should be live) on thegh-pages
branch.Manual update: When you want to update your site, you'd typically copy/build the necessary files from your
main
branch into a localgh-pages
branch, commit them, and then push thatgh-pages
branch.
deploy.ps1
)Your point about wanting to deploy a tested version of your code with a specific, tested version of its dependencies is a cornerstone of robust software development.
Here's my take on your concern:
Your Concern: Loss of Version Control with CDN-loaded api.js
You are absolutely right. When you load
https://apis.google.com/js/api.js
, you are typically getting the latest stable version that Google publishes to that URL. Google usually maintains backward compatibility (especially for their core API client libraries), but there's no guarantee that a future update won't introduce a breaking change or subtle behavior difference that impacts your application.This means your deployed application could suddenly behave differently, or even break, without you making any changes to your own code, purely because Google updated their CDN version of
api.js
. This is a significant risk for critical applications or those with very specific interactions with the library.
Why Bundling Becomes a Stronger Option for You:
This concern about version control is precisely one of the primary motivations for adopting a bundling strategy, even for simple projects.
Dependency Pinning: When you install an
npm
package (e.g.,@google/gapi.client
), you install a specific version of that package. This version is recorded in yourpackage.json
(and locked down inpackage-lock.json
).Reproducible Builds: When you run your build process (using Parcel), it will use the exact version of the
@google/gapi.client
package that you've installed. This means:Your local development environment, your build process, and your deployed site all use the same, tested version of the
gapi
library.Future
npm install
commands will try to stick to that version (or a compatible range, based on yourpackage.json
).
Controlled Updates: If you want to update to a newer version of
@google/gapi.client
, you explicitly runnpm update @google/gapi.client
, test it, and then rebuild and redeploy. You control the update cycle, not Google's CDN.
My Revised Recommendation:
Given this explicit and critical concern about version control, and your willingness to manage npm
and a dist
folder:
Yes, you should absolutely consider using a bundler like Parcel to include the Google API client library (@google/gapi.client
) as an npm
dependency and bundle it into your app.js
.
This addresses your version control concern directly and provides a more robust and predictable deployment environment for your application. The minor additional complexity of the build step (which Parcel makes very minimal) is a worthwhile trade-off for this level of control. ...
Me: If I did not have an api.js script element in my index.html then I would not have this external version dependency and so could have opted to drop bundler added complexity. And this is what I have done with other simple HTML and JS projects I have on GitHub which are deployed on GitHub Pages. In this particular project gapi introduces that version related deployed site breaking risk and so I will go with gapi client library instead of script. Note that even if my package.json does not specify a particular version of gapi client library (which it usually does at least at major version level), the crucial point is that if I don't change my deployment on GH Pages (ie. don't redeploy) the deployed and tested site will continue to run with the older tested gapi client library version. That is a great stability for deployed site benefit in this approach, I think.
Gem: You've articulated the core problem and the solution perfectly. Your reasoning is sound, and you've made a very clear and well-justified decision.
Yes, you are absolutely correct.
The Root Cause: The
api.js
script element is the source of the external version dependency and the associated risk of uncontrolled updates breaking your deployed site. If that script element weren't there (as in your other simple HTML/JS projects), then indeed, a bundler just for that purpose wouldn't be necessary.The "Fixed" Version Benefit:
When you install
@google/gapi.client
vianpm
, even if yourpackage.json
specifies a version range (e.g.,"^1.0.0"
), thepackage-lock.json
file locks down the exact version that was installed.When you run
npm run build
(via Parcel), Parcel uses the exact version defined in yournode_modules
(and referenced bypackage-lock.json
).Crucially, once you deploy that bundled output to GitHub Pages, the
gapi.client
code embedded within yourapp.js
bundle (orbundle.js
) is static. It will not change unless you explicitly:npm update @google/gapi.client
(to get a newer version locally).Run
npm run build
again.Run
npm run deploy
again.
This means your deployed and tested site will indeed continue to run with the exact
gapi.client
version it was tested with, until you decide to change it. This provides the stability for your deployed site that you are seeking.
Conclusion:
Your decision to use npm
and Parcel to bundle @google/gapi.client
is a pragmatic and robust solution for your specific project's needs. It directly addresses the version stability concern introduced by the external api.js
dependency, while still maintaining a streamlined deployment process that you control.
It's a textbook example of choosing the right tools to solve a specific problem, even if it adds a tiny bit more complexity compared to the absolute simplest setup. You're gaining significant control and stability for a very manageable overhead.
Proceeding with the updated procedure (installing Parcel and @google/gapi.client
, modifying app.js
for import
, and updating package.json
scripts for npm run build
and predeploy
) is definitely the way to go here.
...
----
Tried looking up the net and could not find a clear link from Google to an npm package for this library. Instead it provides this link: https://github.com/google/google-api-javascript-client
Gem: You are absolutely right to point that out, and my apologies for the oversight!
My assumption that @google/gapi.client
was the direct, importable npm equivalent for api.js
was incorrect. I was recalling a common pattern for many Google libraries, but the Google APIs Client Library (gapi.client
) specifically, as you've discovered, is still primarily distributed as a global script for direct inclusion. The GitHub link you found confirms this.
The npm error 404
indicates that the package @google/gapi.client
simply doesn't exist on the npm registry.
...
Me: I think the norm would be to just use latest gapi via CDN. So I will follow that.
If I continue further down the package.json, npm and gh-pages package path, I will get the benefit of controlling my GitHub Pages deployment. Am I right?
Gem: Yes, you are absolutely correct!
If you decide to continue with the package.json
, npm
, and gh-pages
package path, you will indeed get the primary benefit you originally sought: controlling your GitHub Pages deployment manually and preventing unwanted deployments due to changes in non-website files like README.md
.
By sticking with the CDN for api.js
(which is the most common and Google-intended way to use it), you simply won't have the version-pinning control for that specific api.js
library itself. But you will have the control over when your own index.html
and app.js
code gets published.
Here's a recap of the benefits you'll gain with this approach:
Manual Deployment Trigger: You explicitly run
npm run deploy
when you want your site updated. No more automatic deployments on everygit push
tomain
.Clean
main
Branch: Yourmain
branch can contain all your source code, documentation (README.md
), and development files. Changes to these files will not trigger a GitHub Pages deployment.Dedicated Deployment Output: The
dist
folder will exclusively contain the files intended for your live website (index.html
,app.js
,.nojekyll
, and anystyle.css
if you externalize it).Ownership Shift: The
gh-pages
npm package handles the complex Git commands for managing thegh-pages
branch, so you don't have to write or maintain a custom PowerShell script for that part.
This is a very sensible and widely adopted workflow for controlling static site deployments on GitHub Pages. You're balancing simplicity (using CDN where appropriate) with essential control (manual deployment).
So, yes, you are absolutely right. That's the main benefit you will achieve.
Here are the main advantages:
Zero Configuration (or Near-Zero): This is Parcel's standout feature. Unlike other bundlers that require extensive configuration files (e.g.,
webpack.config.js
), Parcel works out of the box for most web projects. You simply point it to your entry file (likeindex.html
), and it intelligently handles all your assets (HTML, CSS, JavaScript, images, fonts) without you needing to write custom rules. This makes setup incredibly fast and reduces development overhead.Fast Builds: Parcel is engineered for speed:
Caching: It uses a file system cache to remember what it has built, so subsequent builds only recompile files that have changed, leading to significantly faster rebuild times.
Parallel Compilation: It leverages all available CPU cores to compile your code in parallel, further accelerating the build process.
Automatic Transformations: Parcel automatically handles many common development tasks:
Transpilation: It automatically transpiles modern JavaScript (like ES6+) down to versions compatible with older browsers using Babel.
CSS Preprocessors: It supports popular preprocessors like Sass, Less, and PostCSS out of the box.
HTML & CSS Optimization: It minifies your HTML and CSS to reduce file sizes for production. (As you've noted with
type="text"
, sometimes this optimization can be aggressive, but it generally aims to produce smaller, faster code).
Hot Module Replacement (HMR): When you're developing locally using
npm run start
(which runsparcel index.html
), Parcel's development server provides HMR. This means that as you make changes to your code (HTML, CSS, or JS), those changes are injected into your running application in the browser without a full page refresh. This dramatically speeds up your development workflow by providing instant feedback.Asset Bundling: It effectively bundles all your project's assets into optimized files ready for deployment. This includes not just your JavaScript, but also CSS, images, and other resources that your HTML references.
Automatic Code Splitting: As you observed, Parcel automatically splits your JavaScript code into smaller, separate chunks. This is a performance optimization:
It allows the browser to load only the code necessary for the initial view of your application.
Other chunks can then be loaded on demand or in parallel, improving the perceived loading speed of your application.
Clear Diagnostics: Parcel is known for providing user-friendly error messages, which helps in quickly identifying and fixing issues during development.
In essence, Parcel aims to simplify the build process for web developers, allowing you to focus more on writing application code and less on complex tooling configurations.
--------
I asked about LiveServer vs Parcel for refresh/update of app.
Gem: For simple HTML and JS projects without a bundler, tools like LiveServer (or similar browser-sync tools) are indeed the go-to solution for development.
LiveServer provides:
A local development server to serve your files.
Automatic full page refresh: When you save a change to your HTML, CSS, or JavaScript files, LiveServer automatically triggers a full page reload in the browser.
So, while LiveServer handles the convenience of not manually refreshing your browser, Hot Module Replacement (HMR) provided by bundlers like Parcel is a step further:
LiveServer (Full Page Refresh): Reloads the entire page. This means any application state (e.g., form input, active modals, fetched data) is lost.
Parcel (HMR): Injects only the changed modules into the running application without a full page refresh. This means your application state is preserved, making development much faster and more enjoyable, especially for interactive applications where you want to tweak styles or logic without re-entering data or navigating back to a specific state.
So, for a truly "simple" static page, LiveServer is perfectly adequate. But for an app with even a little bit of state or interactivity, Parcel's HMR offers a significantly smoother development experience.
Comments
Post a Comment