vikejs / vike

🔨 Flexible, lean, community-driven, dependable, fast Vite-based frontend framework.
https://vike.dev
MIT License
4.39k stars 352 forks source link

custom pageContextJsonFileExtension #1960

Open chen6516 opened 1 week ago

chen6516 commented 1 week ago

Description

Some proxy servers will recognize json as a static resource. But it is obvious that the ".pageContext.json" generated by vike is not a static resource. In order to bypass the static resource rules of the proxy server, it would be great if the name can be customized. At present, I use patch-package to change "xx.pageContext.json" to "xx.pageContext.jsonx". If the official can customize it, I will be very grateful.

brillout commented 1 week ago

I'd rather try to find a file extension that works for everyone. Is there some kind of standard file extension for dynamic JSON URLs?

brillout commented 1 week ago

At present, I use patch-package to change "xx.pageContext.json" to "xx.pageContext.jsonx"

Can you share that patch?

brillout commented 1 week ago

Some proxy servers will recognize json as a static resource.

Can you elaborate?

chen6516 commented 1 week ago

use this package to create patch: https://www.npmjs.com/package/patch-package

I'd rather try to find a file extension that works for everyone. Is there some kind of standard file extension for dynamic JSON URLs?我宁愿尝试找到适合每个人的文件扩展名。动态 JSON URL 是否有某种标准文件扩展名?

I don't think there is a standard extension name. I use the jsonx extension here because it's my own creation (x means extension)/

chen6516 commented 1 week ago

At present, I use patch-package to change "xx.pageContext.json" to "xx.pageContext.jsonx"

Can you share that patch?

I use this patch-package to ceate patch: https://www.npmjs.com/package/patch-package

the patch file:

diff --git a/node_modules/vike/dist/esm/node/prerender/runPrerender.js b/node_modules/vike/dist/esm/node/prerender/runPrerender.js
index d49a91a..c5f380f 100644
--- a/node_modules/vike/dist/esm/node/prerender/runPrerender.js
+++ b/node_modules/vike/dist/esm/node/prerender/runPrerender.js
@@ -562,7 +562,7 @@ async function writeFiles({ urlOriginal, pageContext, htmlString, pageContextSer
         write(urlOriginal, pageContext, '.html', htmlString, root, outDirClient, doNotCreateExtraDirectory, onPagePrerender, logLevel)
     ];
     if (pageContextSerialized !== null) {
-        writeJobs.push(write(urlOriginal, pageContext, '.pageContext.json', pageContextSerialized, root, outDirClient, doNotCreateExtraDirectory, onPagePrerender, logLevel));
+        writeJobs.push(write(urlOriginal, pageContext, '.pageContext.jsonx', pageContextSerialized, root, outDirClient, doNotCreateExtraDirectory, onPagePrerender, logLevel));
     }
     await Promise.all(writeJobs);
 }
diff --git a/node_modules/vike/dist/esm/shared/getPageContextRequestUrl.js b/node_modules/vike/dist/esm/shared/getPageContextRequestUrl.js
index 72c1781..b164077 100644
--- a/node_modules/vike/dist/esm/shared/getPageContextRequestUrl.js
+++ b/node_modules/vike/dist/esm/shared/getPageContextRequestUrl.js
@@ -3,7 +3,7 @@ export { pageContextJsonFileExtension };
 export { doNotCreateExtraDirectory };
 // This module isn't loaded by the client-side of Server Routing => we don't inlcude `urlToFile` to `./utils.ts`
 import { urlToFile } from '../utils/urlToFile.js';
-const pageContextJsonFileExtension = '.pageContext.json';
+const pageContextJsonFileExtension = '.pageContext.jsonx';
 // `/some-base-url/index.pageContext.json` instead of `/some-base-url.pageContext.json` in order to comply to common reverse proxy setups, see https://github.com/vikejs/vike/issues/443
 const doNotCreateExtraDirectory = false;
 // See node/renderPage/handlePageContextRequestUrl.ts
chen6516 commented 1 week ago

Some proxy servers will recognize json as a static resource.

Can you elaborate?

As shown in the example in the figure, json is treated as a static resource, so the request will not go through the vike server and the result of the ssr request cannot be obtained.

Therefore, we have to customize xx.pageContext.json to xx.pageContext.jsonx to bypass the limitations of the nginx server. Of course, changing the nginx proxy configuration can solve the json recognition problem, but sometimes developers do not have such authority to change it.

json

brillout commented 1 week ago

Thanks for sharing all that, that's helpful.

So far, there doesn't seem to be a good solution for this. The main issue is that pre-rendered pages should actually generate a .json file (these ones are actually static).

Let us first release the 1.0.0 and let's revisit then. The V1 will include new capabilities that may allow us to fix this in a nice way (e.g. by using a new setting as you suggested).

Related: https://github.com/vikejs/vike/issues/1629