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

fix(router): handle cookie header internally #1276

Merged
merged 3 commits into from
Aug 14, 2024

Conversation

osnoser1
Copy link
Contributor

@osnoser1 osnoser1 commented Aug 14, 2024

PR Checklist

Closes: None.

What is the new behavior?

  • Cookies will be available on load function of .server.ts files without implementing a custom interceptor.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Very small test:

  • In the apps/analog-app/src/app/pages/(home).server.ts file, add a console.log.
image
  • Without this change, if you remove the cookies interceptor of apps/analog-app/src/app/app.config.ts, the console log will print an empty object.

What gif best describes this PR or how it makes you feel?

Copy link

netlify bot commented Aug 14, 2024

Deploy Preview for analog-docs ready!

Name Link
🔨 Latest commit bff4369
🔍 Latest deploy log https://app.netlify.com/sites/analog-docs/deploys/66bc0428ca254e0008fd2b03
😎 Deploy Preview https://deploy-preview-1276--analog-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Aug 14, 2024

Deploy Preview for analog-app ready!

Name Link
🔨 Latest commit bff4369
🔍 Latest deploy log https://app.netlify.com/sites/analog-app/deploys/66bc0428ab370600088067e9
😎 Deploy Preview https://deploy-preview-1276--analog-app.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Aug 14, 2024

Deploy Preview for analog-ng-app ready!

Name Link
🔨 Latest commit bff4369
🔍 Latest deploy log https://app.netlify.com/sites/analog-ng-app/deploys/66bc0428dc00be0008a4ae5b
😎 Deploy Preview https://deploy-preview-1276--analog-ng-app.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Aug 14, 2024

Deploy Preview for analog-blog ready!

Name Link
🔨 Latest commit bff4369
🔍 Latest deploy log https://app.netlify.com/sites/analog-blog/deploys/66bc042886ffbc0008ab4216
😎 Deploy Preview https://deploy-preview-1276--analog-blog.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@brandonroberts
Copy link
Member

What if they already have a "cookie" header? I think the header should have a unique name at least, such as X-Analog-Cookie.

Is cookie one value or a dictionary of key/value cookies?

@osnoser1
Copy link
Contributor Author

What if they already have a "cookie" header? I think the header should have a unique name at least, such as X-Analog-Cookie.

Is cookie one value or a dictionary of key/value cookies?

I was thinking that the cookie header should only be added if the request URL has the format /api/_analog/whatever; I believe it can work in that case.

@osnoser1
Copy link
Contributor Author

osnoser1 commented Aug 14, 2024

The idea is that if we make a request to, for example, http://localhost:4200/posts/my-slug, when the load function of the .server.ts file runs, we can get the cookies using the h3 functions (getCookie, parseCookies, etc.). If we set custom headers, we won't be able to use the h3 cookies functions, we would have to use the headers functions (that it is valid).

The only cookies this request https://github.com/analogjs/analog/blob/beta/packages%2Frouter%2Fsrc%2Flib%2Froute-config.ts#L71 should have are the server request cookies

@osnoser1
Copy link
Contributor Author

osnoser1 commented Aug 14, 2024

cookie

cookie is a complete cookie string, the server with the h3 functions parses internally for us that string

PD: I updated the PR

@@ -26,5 +28,10 @@ export function provideFileRouter(
multi: true,
useValue: () => updateMetaTagsOnRouteChange(),
},
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know there was an internal token for adding root interceptor functions. I'd like to use this for the requestContextInterceptor also, but that's separate from this PR.

@brandonroberts
Copy link
Member

The idea is that if we make a request to, for example, http://localhost:4200/posts/my-slug, when the load function of the .server.ts file runs, we can get the cookies using the h3 functions (getCookie, parseCookies, etc.). If we set custom headers, we won't be able to use the h3 cookies functions, we would have to use the headers functions (that it is valid).

The only cookies this request https://github.com/analogjs/analog/blob/beta/packages%2Frouter%2Fsrc%2Flib%2Froute-config.ts#L71 should have are the server request cookies

I see, that makes sense. We can keep it scoped to start as its only for internal Analog requests.

@brandonroberts brandonroberts changed the title feat(router): handle cookie header internally fix(router): handle cookie header internally Aug 14, 2024
@brandonroberts brandonroberts merged commit 838646c into analogjs:beta Aug 14, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants