diff --git a/Classes/NodeRendering/NodeRenderingUriService.php b/Classes/NodeRendering/NodeRenderingUriService.php index 20e666a..0af8708 100644 --- a/Classes/NodeRendering/NodeRenderingUriService.php +++ b/Classes/NodeRendering/NodeRenderingUriService.php @@ -6,6 +6,8 @@ use Flowpack\DecoupledContentStore\Exception; use Flowpack\DecoupledContentStore\NodeRendering\Extensibility\DocumentRendererInterface; +use Flowpack\DecoupledContentStore\NodeRendering\Render\DocumentRenderer; +use Flowpack\DecoupledContentStore\Transfer\Resource\Target\MultisiteFileSystemSymlinkTarget; use GuzzleHttp\Psr7\ServerRequest; use Neos\ContentRepository\Domain\Model\NodeInterface; use Neos\Flow\Annotations as Flow; @@ -19,6 +21,7 @@ use Neos\Flow\Mvc\Controller\ControllerContext; use Neos\Flow\Mvc\Routing\Dto\RouteParameters; use Neos\Flow\Mvc\Routing\UriBuilder; +use Neos\Flow\ResourceManagement\ResourceManager; use Neos\Flow\Security\Context as SecurityContext; use Neos\Neos\Domain\Model\Site; use Neos\Neos\Service\LinkingService; @@ -46,6 +49,12 @@ final class NodeRenderingUriService #[Flow\Inject] protected SecurityContext $securityContext; + #[Flow\Inject] + protected ResourceManager $resourceManager; + + #[Flow\InjectConfiguration('nodeRendering.useRelativeResourceUris')] + protected bool $useRelativeResourceUris; + /** * @param NodeInterface $node * @param array $arguments @@ -96,12 +105,13 @@ public function buildNodeUri(NodeInterface $node, array $arguments): string } /** - * @param string $uri - * @param NodeInterface $node * @param array $arguments - * @return ControllerContext */ - public function buildControllerContextAndSetBaseUri(string $uri, NodeInterface $node, array $arguments = []) + public function buildControllerContextAndSetBaseUri( + string $uri, + NodeInterface $node, + array $arguments = [], + ): ControllerContext { $request = $this->buildFakeRequest($uri, $node); if (isset($arguments['@format'])) { @@ -112,6 +122,7 @@ public function buildControllerContextAndSetBaseUri(string $uri, NodeInterface $ // as this is now (Flow 6.x) used in the UriBuilder to determine the domain. $baseUri = rtrim(RequestInformationHelper::generateBaseUri($request->getHttpRequest())->__toString(), '/'); ObjectAccess::setProperty($this->baseUriProvider, 'configuredBaseUri', $baseUri, true); + $this->injectResourceBaseUri($node); ObjectAccess::setProperty($this->securityContext, 'initialized', true, true); $this->securityContext->setRequest($request); @@ -120,6 +131,34 @@ public function buildControllerContextAndSetBaseUri(string $uri, NodeInterface $ return new ControllerContext($request, new ActionResponse(), new Arguments([]), $uriBuilder); } + /** + * Gives resource targets without a base URI override the site's domain, like in + * {@see DocumentRenderer::renderDocumentView()}. + * + * Without an override, Flow's FileSystemTarget joins the base URI set above (no trailing slash) directly with + * "_Resources/..." and caches that result for the rest of the process, so all following documents get the same + * broken host. Targets that already have an override keep it: callers like DocumentRenderer, or exports that + * want relative URIs, set it explicitly. + */ + private function injectResourceBaseUri(NodeInterface $node): void + { + /** @var Site|null $currentSite */ + $currentSite = $node->getContext()->getCurrentSite(); + $domain = $currentSite?->getFirstActiveDomain(); + if ($domain === null) { + return; + } + // same normalization as MultisiteFileSystemSymlinkTarget::injectBaseUriIntoRelevantResourcePublishingTargets() + $baseUri = rtrim($this->useRelativeResourceUris ? '' : (string) $domain, '/') . '/'; + + foreach ($this->resourceManager->getCollections() as $collection) { + $target = $collection->getTarget(); + if ($target instanceof MultisiteFileSystemSymlinkTarget && !$target->hasOverrideHttpBaseUri()) { + $target->setOverrideHttpBaseUri($baseUri); + } + } + } + private function buildFakeRequest(string $uri, NodeInterface $node): ActionRequest { $_SERVER['FLOW_REWRITEURLS'] = '1'; diff --git a/Classes/Transfer/Resource/Target/MultisiteFileSystemSymlinkTarget.php b/Classes/Transfer/Resource/Target/MultisiteFileSystemSymlinkTarget.php index ef9458f..2e7737b 100644 --- a/Classes/Transfer/Resource/Target/MultisiteFileSystemSymlinkTarget.php +++ b/Classes/Transfer/Resource/Target/MultisiteFileSystemSymlinkTarget.php @@ -51,6 +51,11 @@ protected function getResourcesBaseUri() return $this->overrideHttpBaseUri . $this->baseUri; } + public function hasOverrideHttpBaseUri(): bool + { + return $this->overrideHttpBaseUri !== null; + } + /** * @param string $overrideHttpBaseUri */