itsgoingd / clockwork

Clockwork - php dev tools in your browser - server-side component
https://underground.works/clockwork
MIT License
5.62k stars 323 forks source link

Implementing on project using Laravel back-end API with SSR SPA #593

Open kdevan opened 2 years ago

kdevan commented 2 years ago

I'm having trouble getting profiling and the toolbar working. I'm pretty sure the issue is with cookies. I have everything using the latest versions, Laravel 9 with PHP 8.1.

  1. Client request hits node.js server
  2. Node.js server makes an API request to a Laravel endpoint to get some data that the site needs
  3. The Laravel endpoint sends back data along with session cookies which will then get set on the client
  4. The Node.js server uses the data to render the page on the client along with passing the cookies
  5. All API requests from the Node.js server or the client app send all cookies back to server on every request

The main point here is that there is shared session state between back-end api and client through cookies being passed back and forth.

I'm pretty sure the issue is that the XDEBUG_PROFILE and clockwork-profile cookies are set with the Domain set to the full domain of the Clockwork app (in my case api.website.com). But in a set up like this where you have something like api.website.com for back-end api and then website.com for client, the cookie domain should be set to the root domain, website.com, so that cookies can be shared with all sub-domains (in this case api.website.com).

I'm wondering if there's a way to control how these cookies are set or if I could set them myself? I'm definitely willing to dig in to see where and how these cookies are set but I thought I'd ask to see someone might have some useful information about this off hand.

I'm hoping that I can profile the API requests that the node.js server is making and the client side metrics and toolbar seem really useful as well. I saw that XHR might have issues but I'm not so concerned with that, just the process up until the page is rendered.

itsgoingd commented 2 years ago

Hey, yeah, the cookies being set for a wrong domain might be the issue.

For the XDEBUG_PROFILE cookie you can absolutely set it yourself, or even trigger the profiling via a GET/POST parameter or enable profiling for all requests (it slows down the response time a lot though). This is an Xdebug feature, not a specifically Clockwork thing - https://xdebug.org/docs/profiler#starting.

For the x-clockwork cookie I guess we could make the domain configurable. Meanwhile you can set the cookie yourself by replicating what Clockwork does - https://github.com/itsgoingd/clockwork/blob/master/Clockwork/Support/Laravel/ClockworkSupport.php#L427-L439.

kdevan commented 2 years ago

Just wanted to thank you for the tips and direction, very much appreciated. This has been on my mind to test out. When I'm back to profiling I'll update here and close the issue.