use "jsdelivr" to host file

hustnzj發表於2022-09-14

From the official features page of jsdelivr, we know that jsDelivr provides mirrors for npm, GitHub, WordPress plugins, and custom endpoints for several other projects with special requirements.

We will go through the Github and npm section.

There aremany kind of URLs on jsdelivr but as we only want to serve a css file for markdown preview in VSCode, so we only focus on the specific file way.

Github

The detail steps:

  • Create a new repository named cdn on Github.
  • Author the relevant css file named md.css which content can be found in serve a css file for markdown preview in VSCode.
  • Git Push to Github.
  • The cdn URL should be https://cdn.jsdelivr.net/gh/<github-username>/<repo-name>@<commit-hash>/css/md.css.

Note:

  • The file path is only specific for my case, change your path or name if necessary.
  • If you don’t use the <commit-hash> way, cdn will cache your css file which will result in confusion if you change you css and push again. There is no hook to purge the cache on cdn now. Except for this, I think this way is easier than npm.

npm

Instead of creating a repository on Github, you should publish a package on npmjs.com.

And in addition to authoring the css file, you should provide the entry point file index.js at the root directory of your package and import the css file.

import "css/md.css"

Then, the cdn URL should be https://cdn.jsdelivr.net/npm/@<package-name>/css/md.css

Note: Publishing updated packages will be rejected if you don’t change your package version, so it’s no cache problem mentioned above.

本作品採用《CC 協議》,轉載必須註明作者和本文連結
日拱一卒

相關文章