-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ETag headers for served javascript files (#39251)
- Loading branch information
Showing
6 changed files
with
170 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Teleport | ||
// Copyright (C) 2024 Gravitational, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package web | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMakeCacheHandler(t *testing.T) { | ||
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
etag := "test-etag" | ||
|
||
recorder := httptest.NewRecorder() | ||
|
||
req, err := http.NewRequest("GET", "/testfile.woff", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cacheHandler := makeCacheHandler(testHandler, etag) | ||
|
||
cacheHandler.ServeHTTP(recorder, req) | ||
|
||
expectedCacheControl := "max-age=" + strconv.Itoa(int(time.Hour*24*365/time.Second)) + ", immutable" | ||
require.Equal(t, expectedCacheControl, recorder.Header().Get("Cache-Control")) | ||
|
||
req2, err := http.NewRequest("GET", "/testfile.css", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cacheHandler.ServeHTTP(recorder, req2) | ||
|
||
require.Equal(t, etag, recorder.Header().Get("ETag")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { createHash } from 'crypto'; | ||
import { writeFileSync } from 'fs'; | ||
import { resolve } from 'path'; | ||
|
||
// this plugin is used to generate a file containing the hash of the app.js | ||
// bundle. Because we omit the hash from the filename, we don't have access to it | ||
// in the build chain. We generate a hash using the same methods used when files | ||
// passed by default to `augmentChunkHash` (hash.update). | ||
// https://rollupjs.org/plugin-development/#augmentchunkhash | ||
export function generateAppHashFile(outputDir: string, entryFilename: string) { | ||
return { | ||
name: 'app-hash-plugin', | ||
generateBundle(_, bundle) { | ||
// bundle is OutputChunk | OutputAsset. These types aren't exported | ||
// by vite but by rollup, which isn't directly in our bundle so we | ||
// will use `any` instead of installing rollup | ||
// https://rollupjs.org/plugin-development/#generatebundle | ||
const { code } = bundle[entryFilename] as any; | ||
if (code) { | ||
const hash = createHash('sha256').update(code).digest('base64'); | ||
writeFileSync(resolve(outputDir, 'apphash'), hash); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters