-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dev/plugin)!: redo reverse proxy setup (#376)
Prior to this change, assets were imported relatively, also taking into account relativePathToPrefix. While this generally worked, there were other issues due to Vite's build optimizations: specifically module preloading and inline assets. Due to this we decided to take a new approach where assets are now imported absolutely and it's now up to the RP config to handle the assets. A working example of a Netlify setup for a RP at the `subdirectory` subfolder looks like this: ``` [[redirects]] from = "/subdirectory/assets/*" to = "https://rp-test.pagesprod.yextengtest.com/subdirectory/assets/:splat" status = 200 force = true headers = {Host = "https://rp-test.pagesprod.yextengtest.com/"} [[redirects]] from = "/subdirectory/*" to = "https://rp-test.pagesprod.yextengtest.com/:splat" status = 200 force = true headers = {Host = "https://rp-test.pagesprod.yextengtest.com/"} [[redirects]] from = "/subdirectory" to = "https://rp-test.pagesprod.yextengtest.com/index.html" status = 200 force = true headers = {Host = "https://rp-test.pagesprod.yextengtest.com/"} ``` In order to properly configure the Vite side, a user must set the assetsDir in the vite.config.js (`subdirectory/assets` for the above example) or in the config.yaml (which will be globally supported in some upcoming PRs). Other changes in the PR include a massive refactor of ProjectStructure in order to simplify its usage and is now being used everywhere, meaning that all filenames and folders are fully configurable. --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4cf67c9
commit a640802
Showing
45 changed files
with
851 additions
and
496 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,6 +346,36 @@ THE SOFTWARE. | |
|
||
----------- | ||
|
||
The following npm package may be included in this product: | ||
|
||
- [email protected] | ||
|
||
This package contains the following license and notice below: | ||
|
||
(The MIT License) | ||
|
||
Copyright (C) 2011-2015 by Vitaly Puzrin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
|
||
----------- | ||
|
||
The following npm package may be included in this product: | ||
|
||
- [email protected] | ||
|
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
42 changes: 42 additions & 0 deletions
42
packages/pages/src/common/src/assets/getAssetsFilepath.test.ts
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,42 @@ | ||
import { determineAssetsFilepath } from "./getAssetsFilepath.js"; | ||
import * as importHelper from "./import.js"; | ||
|
||
describe("getAssetsFilepath - determineAssetsFilepath", () => { | ||
it("returns assets when no files defined", async () => { | ||
const actual = await determineAssetsFilepath("assets", "", ""); | ||
|
||
expect(actual).toEqual("assets"); | ||
}); | ||
|
||
it("returns custom assetsDir from config.yaml", async () => { | ||
const actual = await determineAssetsFilepath( | ||
"assets", | ||
"tests/fixtures/config.yaml", | ||
"tests/fixtures/vite.config.js" | ||
); | ||
|
||
expect(actual).toEqual("subpath/assets"); | ||
}); | ||
|
||
it("returns custom assets from vite config when no config.yaml", async () => { | ||
const viteConfig = { | ||
default: { | ||
plugins: [], | ||
build: { | ||
assetsDir: "viteConfigAssetsDir", | ||
}, | ||
}, | ||
}; | ||
|
||
const importSpy = jest.spyOn(importHelper, "import_"); | ||
importSpy.mockImplementation(async () => viteConfig); | ||
|
||
const actual = await determineAssetsFilepath( | ||
"assets", | ||
"tests/fixtures/invalid.yaml", | ||
"does not matter since mocked" | ||
); | ||
|
||
expect(actual).toEqual("viteConfigAssetsDir"); | ||
}); | ||
}); |
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,39 @@ | ||
import fs from "node:fs"; | ||
import { pathToFileURL } from "url"; | ||
import { UserConfig } from "vite"; | ||
import { import_ } from "./import.js"; | ||
import yaml from "js-yaml"; | ||
import { ConfigYaml } from "../config/config.js"; | ||
|
||
/** | ||
* Determines the assets directory to use by checking the following, in order: | ||
* 1. config.yaml | ||
* 2. vite.config.json assetDir | ||
* 3. default to "assets" | ||
* @param servingJsonPath the path to sites-config/serving.json - make sure to take scope into account | ||
* @param viteConfigPath the path to vite.config.js | ||
*/ | ||
export const determineAssetsFilepath = async ( | ||
defaultAssetsDir: string, | ||
configYamlPath: string, | ||
viteConfigPath: string | ||
) => { | ||
if (configYamlPath === "" || viteConfigPath === "") { | ||
return defaultAssetsDir; | ||
} | ||
|
||
if (fs.existsSync(configYamlPath)) { | ||
const configYaml = yaml.load( | ||
fs.readFileSync(configYamlPath, "utf-8") | ||
) as ConfigYaml; | ||
|
||
if (configYaml.assetsDir && configYaml.assetsDir !== "") { | ||
return configYaml.assetsDir; | ||
} | ||
} | ||
|
||
const viteConfig = await import_(pathToFileURL(viteConfigPath).toString()); | ||
const userConfig = viteConfig.default as UserConfig; | ||
|
||
return userConfig.build?.assetsDir ?? defaultAssetsDir; | ||
}; |
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,6 @@ | ||
/** | ||
* A custom import function so that it can be mocked in tests. | ||
*/ | ||
export const import_ = async (filepath: string) => { | ||
return await import(filepath); | ||
}; |
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,7 @@ | ||
export interface ConfigYaml { | ||
/** | ||
* The folder path that assets will be served from. If your using a reverse proxy at | ||
* a subpath, this should typically look like "mySubpath/assets". | ||
* */ | ||
assetsDir?: string; | ||
} |
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
7 changes: 2 additions & 5 deletions
7
packages/pages/src/common/src/function/internal/getFunctionFilepaths.test.ts
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
Oops, something went wrong.