Skip to content

Enable support for custom defined CDN endpoints - #97

Merged
mcampa merged 3 commits into
bigcommerce:masterfrom
dstaley:master
Sep 28, 2016
Merged

Enable support for custom defined CDN endpoints#97
mcampa merged 3 commits into
bigcommerce:masterfrom
dstaley:master

Conversation

@dstaley

@dstaley dstaley commented Sep 12, 2016

Copy link
Copy Markdown
Contributor

This pull request allows theme creators to define custom CDN endpoints, and use them via the cdn handlebars helper.

When designing themes for BigCommerce, theme designers might want to include large, high resolution image assets as components of their theme. For example on maruccisports.com, each product category includes a 5120x2536 background image, most of which are between 3 and 5 MB. Since sending that much image data down the wire is a poor user experience, we use Imgix to resize and reformat the images depending on the device viewing them. A theme designer might want to use a local version of the image in development, and a version on a CDN like Imgix in production. This pull request allows the designer to define endpoints in their theme's config.json file like this:

{
  "name": "Cornerstone",
  "version": "1.3.2",
  "settings": {
    "homepage_new_products_count": 12,
    "homepage_featured_products_count": 8,
    "cdn": {
      "customcdn": {
        "path": "https://bigcommerce.customcdn.net"
      }
    }
  }
}

After defining an endpoint, the theme designer is then able to use the shortname the same way they currently use the webdav one:

<img src="{{cdn "customcdn:img/image.jpg"}}" />

In development, the previous helper would return the following:

<img src="/assets/cdn/customcdn/img/image.jpg" />

And in production:

<img src="https://bigcommerce.customcdn.net/img/image.jpg" />

Right now, the helper is configured to rewrite local URLs to a cdn folder within assets. This is to circumvent the 50MB bundle size limit by excluding assets/cdn from the bundle created by stencil bundle. In addition to this PR, I've filed one that makes this change against the stencil-cli repo. bigcommerce/stencil-cli#240

Comment thread test/helpers/cdn.js Outdated
};
var themeSettings = {
cdn: {
customcdn: {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this structure is unnecessary, the path is too nested. Can you change the code to be like this?

"settings": {
      cdn: {
        customcdn1: "cdn1.mystore.com",
        customcdn2: "cdn2.mystore.com"
    }
}

usage:

{{cdn "customcdn1:path/to/image.jpg"}}
{{cdn "customcdn2:path/to/image.jpg"}}

Also, if you can, add a test case with two custom cdn urls like the above

@dstaley dstaley Sep 27, 2016

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely agree that the additional nesting is unnecessary, but I was thinking it would be better to start out with an object instead of a string in the event additional functionality like different endpoints based on environment. For example, Imgix can resize images based on URL parameters, but if you wanted to test this functionality locally you'd need to run an additional server, and setup something like this

cdn: {
    imgix: {
        path: "https://bc.imgix.net",
        local_path: "http://localhost:8080"
    }
}

However, starting with using strings and then just adding a typecheck in the future if additional functionality is added could be a way around it as well! Just let me know what you'd prefer and I'll make it happen!

I'll also push a few additional tests with multiple endpoints configured.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point, but we want to keep the settings very simple and clean. If we want to implement the different environments idea we could do the type check as you said.

also adds additional tests for when multiple CDN endpoints are
configured
@mcampa

mcampa commented Sep 28, 2016

Copy link
Copy Markdown
Contributor

LGTM 👍

need to test it before deploying to production @bc-ranji

@mcampa
mcampa merged commit e8cbfcf into bigcommerce:master Sep 28, 2016
@bookernath

Copy link
Copy Markdown
Contributor

Dude, sweet.

@imagekit-developer

Copy link
Copy Markdown

Does it only work with cdn helper? How to do the same with getImage helper?

@mcampa

mcampa commented Sep 15, 2017

Copy link
Copy Markdown
Contributor

getImage only works with images hosted by BigCommerce so it won't work with custom cdns.

@imagekit-developer

imagekit-developer commented Sep 16, 2017

Copy link
Copy Markdown

Is there any possible way so that one can use custom CDN for the URLs generated via getImage?

Maybe some sort of assignment and then replacing in the template. Not sure if that's possible in the handlebar.

@mcampa

mcampa commented Sep 20, 2017

Copy link
Copy Markdown
Contributor

there is a replace helper. But again, getImage is a helper to resize product images hosted by BigCommerce. Images are processed in our servers and hosted in our cdn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants