Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:55:59+00:00 2026-06-14T01:55:59+00:00

I want my static content (images, javascript files, css files etc) to served in

  • 0

I want my static content (images, javascript files, css files etc) to served in full only after the file has been updated.

If a file has not changed since it was last requested (as determined by the ETag and Last-Modified response header values) then I want the cached versions of the files to be used by the client browser.

Does Nancy support this functionality?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:56:00+00:00Added an answer on June 14, 2026 at 1:56 am

    Nancy does partially support the ETag and the Last-Modified headers. It sets them for all static files but as of version 0.13 it does nothing with these values. here is the Nancy code:

    Nancy.Responses.GenericFileResponse.cs

    if (IsSafeFilePath(rootPath, fullPath))
    {
        Filename = Path.GetFileName(fullPath);
    
        var fi = new FileInfo(fullPath);
        // TODO - set a standard caching time and/or public?
        Headers["ETag"] = fi.LastWriteTimeUtc.Ticks.ToString("x");
        Headers["Last-Modified"] = fi.LastWriteTimeUtc.ToString("R");
        Contents = GetFileContent(fullPath);
        ContentType = contentType;
        StatusCode = HttpStatusCode.OK;
        return;
    }
    

    To make use of the ETag and Last-Modified header values you need to add a couple of modified extensions methods. I borrowed these directly from the Nancy source code in GitHub (as this functionality is planned for a future release) but the original idea came from Simon Cropp – Conditional responses with NancyFX

    Extension Methods

    public static void CheckForIfNonMatch(this NancyContext context)
    {
        var request = context.Request;
        var response = context.Response;
    
        string responseETag;
        if (!response.Headers.TryGetValue("ETag", out responseETag)) return;
        if (request.Headers.IfNoneMatch.Contains(responseETag))
        {
            context.Response = HttpStatusCode.NotModified;
        }
    }
    
    public static void CheckForIfModifiedSince(this NancyContext context)
    {
        var request = context.Request;
        var response = context.Response;
    
        string responseLastModified;
        if (!response.Headers.TryGetValue("Last-Modified", out responseLastModified)) return;
        DateTime lastModified;
    
        if (!request.Headers.IfModifiedSince.HasValue || !DateTime.TryParseExact(responseLastModified, "R", CultureInfo.InvariantCulture, DateTimeStyles.None, out lastModified)) return;
        if (lastModified <= request.Headers.IfModifiedSince.Value)
        {
            context.Response = HttpStatusCode.NotModified;
        }
    }
    

    Finally you need to call these methods using the AfterRequest hook in your Nancy BootStrapper.

    BootStrapper

    public class MyBootstrapper :DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            pipelines.AfterRequest += ctx =>
            {
                ctx.CheckForIfNoneMatch();
                ctx.CheckForIfModifiedSince();
            };
            base.ApplicationStartup(container, pipelines);
        }
        //more stuff
    }
    

    Watching the responses with Fiddler you will see the first hit to your static files downloads them with a 200 - OK Status Code.

    Thereafter each request returns a 304 - Not Modified Status Code. After a file is updated, requesting it once again downloads it with a 200 - OK Status Code … and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For testing purposes I want to use Jetty 8 to serve only static content.
.button { background: url(../Images/button.png); } Problem: for performance reason all static content has expiration
I want to give a static javascript block of code to a html template
I want to call a static method in my jsp file using scriptlet and
In a system that I'm building I want to serve Static files (static HTML
I'm working through a problem where I want to select a different static content
I've been reading articles about speeding up websites by serving static content from a
I'm currently experiencing problems with static content - most noticeably jQuery datepicker images, but
I am using the Gallery3D -Code but want it to only display images from
I'm currently facing the problem that I want to serve static content via a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.