source

IIS를 통해 제공되는 단일 페이지 응용 프로그램 HTML 파일의 캐시를 비활성화하려면 어떻게 해야 합니까?

itover 2023. 3. 28. 21:41
반응형

IIS를 통해 제공되는 단일 페이지 응용 프로그램 HTML 파일의 캐시를 비활성화하려면 어떻게 해야 합니까?

IIS를 통해 제공되는 단일 페이지 애플리케이션(angular-js)이 있습니다.HTML 파일의 캐싱을 방지하려면 어떻게 해야 합니까?관리 콘솔을 통해 IIS에 액세스할 수 없으므로 index.html 또는 web.config 중 하나의 콘텐츠를 변경하여 솔루션을 구현해야 합니다.

현재 조사 중인 옵션은 다음과 같습니다.

IIS는 버전 7.5 입니다.NET 프레임워크 4

다음 추가web.configChrome, IE, Firefox 및 Safari에서 작동하는 솔루션:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <location path="index.html">
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Cache-Control" value="no-cache" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>

</configuration>

이를 통해 다음 사항을 확실하게 할 수 있습니다.Cache-Control헤더는 로 설정됩니다.no-cache요청할 때index.html.

.NET Core의 경우 다음을 사용했습니다.

        app.UseStaticFiles(new StaticFileOptions
        {
            OnPrepareResponse = context =>
            {                   
                if (context.File.Name == "index.html" ) {
                    context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
                    context.Context.Response.Headers.Add("Expires", "-1");
                }
            }
        });

ASP에서 브라우저 캐시를 비활성화하는 방법에 대한 크레딧입니다.NET core rc2?

html 파일을 제공할 때 임의 쿼리 문자열을 추가할 수 있습니다.이렇게 하면 파일이 브라우저 캐시에 있더라도 브라우저가 이전 버전을 사용할 수 없습니다.

/index.html?rnd=timestamp

다른 옵션은 캐시 없음 설정을 IIS 수준에서 추가하는 것입니다.이것에 의해, 응답에 캐시 제어:no-cache 가 추가되어 브라우저가 파일을 캐시하지 않게 됩니다.IIS 7 이후부터 동작합니다.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <!-- Note the use of the 'location' tag to specify which 
       folder this applies to-->
  <location path="index.html">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>
  <meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0">

SPA의 주요 전제는 Never cache index.html 입니다.

MDN 권장사항에 따라 캐시를 방지하려면 no-store max-age=0 값을 가진 Cache-Control HTTP 헤더를 리소스에 추가해야 합니다(이 경우 index.timeout 파일).

캐시가 없는 대신 저장소가 없는 이유는 무엇입니까?

저장소가 없는 경우 리소스가 저장되지 않습니다.no-cache를 사용하면 리소스를 저장할 수 있지만 사용하기 전에 저장소에서 서버에서 검증해야 합니다.

max-age=0일까요?

기존의 유효한 캐시 응답을 강제로 클리어합니다(스토어 없음).

IIS에서는 web.config 파일을 통해 앱 캐시 구성을 관리할 수 있습니다.다음으로 index.html 파일의 캐시 설정과 루트 설정을 포함한 완전한 web.config 파일(애플리케이션의 루트디렉토리에 배치해야 합니다)을 나타냅니다(예로서 SPA 라우팅과 HTTP 리다이렉션을 HTTPS에 추가했습니다).

<configuration>
  <location path="index.html">
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Cache-Control" value="no-store, max-age=0" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
        </rule>
        <rule name="SPA Routes" enabled="true" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

@MichailMichailidis가 언급했듯이 SPA의 모든 요청에는 가상 URL이 있으며 실제 위치 경로에 매핑되지 않기 때문에 이러한 답변은 모두 효과가 없었습니다.location path 데 없다index.html.

이 답변에 링크되어 있는 이 블로그 투고에서 해결책을 찾았습니다.둘 다 리라이트모듈을 사용하여 리라이트모듈을 사용하여cache-control응답 헤더를 지정합니다.

이 설정 후의 web.config는 다음과 같습니다.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <outboundRules>
        <rule name="RewriteCacheControlForIndexHtmlFile" 
          preCondition="IsIndexHtmlFile">
          <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
          <action type="Rewrite" value="max-age=0" />
        </rule>
        <preConditions>
          <preCondition name="IsIndexHtmlFile">
            <add input="{REQUEST_FILENAME}" pattern="index.html" />
          </preCondition>
        </preConditions>
      </outboundRules>
...

언급URL : https://stackoverflow.com/questions/26600845/how-to-disable-caching-of-single-page-application-html-file-served-through-iis

반응형