How is codeload.github.com different to api.github

2020-07-20 03:36发布

问题:

I was browsing the next.js repository and noticed this function that downloads and extracts a template from GitHub, with tar:

export async function downloadAndExtractExample(
  root: string,
  name: string
): Promise<void> {
  return await promisePipe(
    got.stream('https://codeload.github.com/zeit/next.js/tar.gz/canary'),
    tar.extract({ cwd: root, strip: 3 }, [`next.js-canary/examples/${name}`])
  )
}

I searched on StackOverflow and I only found this:

  • GitHub Api download zip or tarball link

That's a thread explaining how you can pull a tar.gz from GitHub, but there is no mention of the "codeload" subdomain. How is it different to "api"?

回答1:

The GitHub API provides the best way to get a URL to download an archive. When you make a GET request on that URL, it will redirect you to a URL on codeload.github.com. codeload is the service which provides archives for download and it's on its own domain for caching reasons.

While it's possible to use the codeload URL directly, you generally want to use the API URL, since it handles things like authentication more gracefully, and the codeload URLs for private repositories are generally ephemeral.