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 8345675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:45:29+00:00 2026-06-09T06:45:29+00:00

It seems somewhat awkward whenever I need to deal with custom responses data type

  • 0

It seems somewhat awkward whenever I need to deal with custom responses data type on Spring MVC.

In my case, I need to return a JavaScript content. However, I want it to be cache.

So to clarify, this is not a static file case (<mvc:resources...), Instead it’s a dynamic file generated on the server, that I do want to cache (i.e. HttpResponse 200 and HttpResponse 302).

In terms of code, on the client side I simply have:

<script src="<spring:url value='/some-file.js'/>"></script>

Than a SpringMVC controller:

@RequestMapping(value = "/some-file.js")
public ResponseEntity<String> resourceBundles(HttpServletRequest request, HttpServletResponse response, Locale locale) throws IOException {

    responseHeaders.add("Cache-Control", "public, max-age");
    responseHeaders.add("Content-Type", "text/javascript; charset=UTF-8");
    responseHeaders.add("Expires", "max-age");

    // Turn this into JSON response:
    String someJson = "{ a:a, b;b};";
    return new ResponseEntity<String>("var data = " + someJson, responseHeaders,  HttpStatus.OK);
}

However, it seems that the browser is always trying to access this dynamic JS file.

Since this file is Session depended, I can not generate it and treat it as a static file.

Any suggestions?

  • 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-09T06:45:31+00:00Added an answer on June 9, 2026 at 6:45 am

    This is the correct behavior. Most browsers will send you a GET request with a If-Modified-Since + time stamp to check whether a file has changed.

    In the usual case, you’d use the time stamp to figure out whether the file has changed. But since it never changes in your case, you can reply with a 304 / HttpStatus.NOT_MODIFIED response without a response body (instead of 200 / OK).

    That tells the browser that the file hasn’t changed.

    This should work:

    @RequestMapping(value = "/some-file.js")
    public ResponseEntity<String> resourceBundles(
        HttpServletRequest request, 
        HttpServletResponse response, Locale locale) throws IOException {
    
        Date lmod = session.getAttribute("lmod");
        if( null == lmod ) {
            lmod = new Date();
            session.setAttribute("lmod", lmod);
        }
    
        responseHeaders.add("Last-Modified", lmod);
    
        String ifModifiedSince = request.getHeader("If-Modified-Since");
        if( null != ifModifiedSince ) { // You may want to compare lmod and ifModifiedSince here, too
            return new ResponseEntity( responseHeaders, HttpStatus.NOT_MODIFIED );
        }
    
        ... create first time response ...
    }
    

    Telling the browser Last-Modified will enable it to send you If-Modified-Since

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

Sidebar

Related Questions

I've noticed that the word monad seems to be used in a somewhat inconsistent
I'm somewhat new to Javascript and I can't seem to figure out exactly what's
Seems like i need some help with a project. I have a routine ,
Seems, it's common task, but I haven't found solution. I need to calculate number
I don't have much programming experience. But, to me, Struct seems somewhat similar to
I'm somewhat new to web development and I need to develop a user system
This question seems somewhat related to How do I check if the mouse is
The SVGPath.getBBox() implementation in at least WebKit seems somewhat shoddy; is there some better
In advance apologize if the question seems somewhat broad or strange, I don't mean
I feel somewhat foolish asking such a simple question, but I can't seem to

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.