Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom pageContextJsonFileExtension #1960

Open
chen6516 opened this issue Nov 13, 2024 · 7 comments
Open

custom pageContextJsonFileExtension #1960

chen6516 opened this issue Nov 13, 2024 · 7 comments
Labels
enhancement ✨ New feature or request

Comments

@chen6516
Copy link

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.

@chen6516 chen6516 added the enhancement ✨ New feature or request label Nov 13, 2024
@brillout
Copy link
Member

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
Copy link
Member

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

Can you share that patch?

@brillout
Copy link
Member

brillout commented Nov 13, 2024

Some proxy servers will recognize json as a static resource.

Can you elaborate?

@chen6516
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Member

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: #1629

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants