Giter Site home page Giter Site logo

webessentials.aspnetcore.cdntaghelpers's Introduction

ASP.NET Core CDN helpers

Build status NuGet

This NuGet package makes it painless to use a Content Delivery Network (CDN) to serve static files from any ASP.NET Core web application no matter where it is hosted.

Using a CDN to serve static resoruces (JS, CSS and image files) can significantly speed up the delivery of content to your users by serving those resources from edge servers located in data centers around the world. This reduces latency by a wide margin.

CDN chart

Using a CDN has never been cheaper and with this NuGet package it is now super easy to set up.

Read more about Content Delivery Networks here.

Getting started

It's easy to use a CDN in your ASP.NET Core web application. Here's how to get started.

1. Setup a CDN

We recommend you use the Azure CDN (try it for free now), but any CDN supporting reverse proxying or origin push will work.

Keep in mind that you don't need to host your website on Azure in order to use the Azure CDN.

When using the Azure CDN, you will get an endpoint URL that looks something like this: https://myname.azureedge.net. You need that URL in step 2.

2. Register the Tag Helpers

Do that by adding this line to the _ViewImports.cshtml file:

@addTagHelper *, WebEssentials.AspNetCore.CdnTagHelpers

Then add he CDN url to the appsettings.json file:

{
  "cdn": {
    "url": "https://myname.azureedge.net"
  }
}

That's it. Now all your static assets are being requested directly from the CDN instead of locally from your website.

3. Verify it works

Run the page in the browser and make sure that all JavaScript, CSS and image references have been modified to now point to the CDN.

You can do that by looking at the HTML source code. There should no longer by any relative file references, like this one:

<script src="js/site.js"></script>

...but instead it should look like this:

<script src="https://myname.azureedge.net/js/site.js"></script>

Dynamic HTML

If HTML is generated from a database or other source where you don't control the markup, you can still cdnify the image elements by using the cdnify attribute like so:

<article class="blogpost" cdnify>
  @Model.Post
</article>

Configuration

Use the appsettings.json file to store the configuration settings.

{
  "cdn": {
    "url": "https://myname.azureedge.net",
    "prefetch": true
  }
}

That makes it easy to disable CDN locally when developing and only enable it in production by adding the config to the appsettings.production.json file.

url: An absolute URL that is used to prefix all static file references with.

prefetch: Will by default inject a DNS prefetch link to the <head> of the document to speed up the DNS resolution. Set to false to disable.

webessentials.aspnetcore.cdntaghelpers's People

Contributors

frederic-cb avatar madskristensen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

webessentials.aspnetcore.cdntaghelpers's Issues

How to ignore some urls

Hi there,

A great piece of code. Is there a way to ignore certain urls from the cdn? i.e. perhaps something like cdnify-ignore etc?

compatible with asp-fallback-src

This is more of a question than an issue, but I'm curious, is this library compatible with the asp-fallback-src data attribute?

Is it possible to set that by default to fall back to the web server if the CDN is unavailable? Would be nice if I did not have to add asp-fallback-src to every HTML tag I would like to fall back to.

Local IIS support ?

Hey there, thanks for brilliant project.
It saves the day.

But as you know, while working with local IIS,
the application url is added app folder's name and app url becomes like :

http://localhost/myappfolder

And when we enable CDN, all CDN urls are generated as :

https://cdndomain.com/myappfolder/site.css

Is there a way to overcome this issue on local IIS ?
The urls should be generated like :

https://cdndomain.com/site.css

Thanks.

StyleTagHelper results in empty <style> tags

Steps to reproduce:

  1. Create new ASP.NET Core web app
  2. Add NuGet reference to CdnTagHelpers 1.0.16
  3. Add @addTagHelper *, WebEssentials.AspNetCore.CdnTagHelpers to _ViewImports.cshtml
  4. Make appsettings.json change as recommended in README
  5. Add the style tag below to Index.cshtml
  6. Run app, view source. Note that the style tag renders as <style></style> and the rule(s) have been removed.
  7. Add @removeTagHelper WebEssentials.AspNetCore.CdnTagHelpers.StyleTagHelper, WebEssentials.AspNetCore.CdnTagHelpers to _ViewImports.cshtml
  8. Refresh page, note that styles return

Add to Index.cshtml:

<style>
    .foo {
        color: blue;
    }
</style>

appsettings.json:

{
  "cdn": {
    "url": "https://myname.azureedge.net"
  }
}

Rendered source with StyleTagHelper enabled:

<style></style>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.