|
| 1 | +.. include:: /Includes.rst.txt |
| 2 | + |
| 3 | +.. _AjaxVersions: |
| 4 | + |
| 5 | +===================== |
| 6 | +AJAX version switcher |
| 7 | +===================== |
| 8 | + |
| 9 | +Rendered documentation provides a version switcher on top of the |
| 10 | +navigation. |
| 11 | + |
| 12 | +For documentation deployed on `docs.typo3.org` clicking on the version |
| 13 | +switcher will perform an AJAX API request that lists all available |
| 14 | +versions and languages for the current documentation. |
| 15 | + |
| 16 | +For local rendering however, there are two issues that will lead to |
| 17 | +the version switcher not working properly: |
| 18 | + |
| 19 | +* Rendered HTML files viewed via the `file:///` notation may not |
| 20 | + execute any JavaScript due to security considerations/configuration |
| 21 | + of your browser. |
| 22 | +* When viewing HTML files via `localhost` or a `.ddev.site` webserver, |
| 23 | + the AJAX call will fail due to `CORS <https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS>`__ |
| 24 | + (Cross-Origin Resource Sharing) security restrictions. |
| 25 | + |
| 26 | +Developers of the `render-guides` project may need to simulate menu rendering though. |
| 27 | + |
| 28 | +For them, navigation can be proxied and simulated. |
| 29 | + |
| 30 | +.. _AjaxVersions-proxy: |
| 31 | + |
| 32 | +Making the proxy available |
| 33 | +========================== |
| 34 | + |
| 35 | +When using the DDEV integration to view the documentation, the DocumentRoot of the |
| 36 | +DDEV webserver is set to `Documentation-GENERATED-temp`. This does not |
| 37 | +contain active PHP files by default. |
| 38 | + |
| 39 | +The file :file:`packages/typo3-docs-theme/assets/js/versions-proxy.php` in the |
| 40 | +repository of this project (`<https://github.com/TYPO3-Documentation/render-guides>`) |
| 41 | +can act as a simple proxy. You can copy or symlink that file into your `Documentation-GENERATED-temp` |
| 42 | +directory, so that it is callable with a URL like: |
| 43 | + |
| 44 | +.. code:: |
| 45 | +
|
| 46 | + https://render-guides.ddev.site/versions-proxy.php?url=https://docs.typo3.org/m/typo3/tutorial-getting-started/12.4/en-us/Concepts/Index.html |
| 47 | +
|
| 48 | +The PHP proxy passes the URL parameter `url` on to the actual `docs.typo3.org` API endpoint, |
| 49 | +and returns its output locally. |
| 50 | + |
| 51 | +Once the proxy PHP file is in place, the default values of the rendering will take effect |
| 52 | +already. See :ref:`AjaxVersions-data-attributes` on how to fine-tune this. |
| 53 | + |
| 54 | +Details on how the version switcher is implemented |
| 55 | +================================================== |
| 56 | + |
| 57 | +Local rendering is automatically detected via the absence of an environment variable |
| 58 | +`TYPO3AZUREEDGEURIVERSION` (see :ref:`deploy-azure-assets`). |
| 59 | + |
| 60 | +This allows a Twig function `isRenderedForDeployment` (defined in |
| 61 | +:file:`packages/typo3-docs-theme/src/Twig/TwigExtension.php` of this repository) to |
| 62 | +conditionally generate output. The Twig template file |
| 63 | +`packages/typo3-docs-theme/resources/template/structure/navigation/navigationHeader.html.twig` |
| 64 | +makes use of that function to define some default HTML data-attributes for out-of-the-box |
| 65 | +version witcher simulation, once the PHP proxy URL can be called without a 404 error. |
| 66 | + |
| 67 | +The JavaScript is contained in :file:`packages/typo3-docs-theme/assets/js/versions.js` and |
| 68 | +contains the code for parsing the JSON response of the API, and sorting the keys appropriately. |
| 69 | +More details can be found inside the code comments of that file. |
| 70 | + |
| 71 | +When changing this JavaScript, the assets must be rebuilt (:bash:`ddev npm-build`) and |
| 72 | +documentation must be rendered with the built assets (:bash:`make docs`). |
| 73 | + |
| 74 | +The JavaScript file also contains some logic to set names for known published translations. |
| 75 | +If a static name is not available, it is resolved to the ISO-code (like "de-de"). The |
| 76 | +TYPO3 Documentation team can not easily access active server-side code to get an automatic |
| 77 | +list of languages. |
| 78 | + |
| 79 | +.. _AjaxVersions-data-attributes: |
| 80 | +Configuring HTML data-attributes |
| 81 | +================================ |
| 82 | + |
| 83 | +The following data-attributes on the HTML element :html:`<div id="toc-version">` are available: |
| 84 | + |
| 85 | +* `data-override-url-self` - This is a full URL starting with `https://docs.typo3.org` which |
| 86 | + is used as the simulated page from which the version switching information is retrieved for. |
| 87 | + For example, `https://docs.typo3.org/m/typo3/tutorial-getting-started/12.4/en-us/Concepts/Index.html`. |
| 88 | + |
| 89 | +* `data-override-url-proxy` - Points to the full URI of the :ref:`AjaxVersions-proxy` PHP proxy. |
| 90 | + This is called due to CORS reasons. The default of this attribute is set to |
| 91 | + `https://render-guides.ddev.site/versions-proxy.php?url=`. Unless this file is manually |
| 92 | + copied to this URL, the proxy will return a 404 failure and inform about the non-working |
| 93 | + version switcher. |
| 94 | + |
| 95 | +Seeing the simulated output |
| 96 | +=========================== |
| 97 | + |
| 98 | +Once the PHP proxy is in place, open the rendered documentation HTML file in your DDEV/webserver |
| 99 | +environment (not using a `file:///` syntax, because then JavaScript may unavailable), for example |
| 100 | + |
| 101 | +.. code:: |
| 102 | +
|
| 103 | + https://render-guides.ddev.site/Developer/AjaxVersions.html |
| 104 | +
|
| 105 | +You can then also use the browser's JavaScript console to manipulate the used URLs for |
| 106 | +rendering, for example: |
| 107 | + |
| 108 | +.. code:: |
| 109 | +
|
| 110 | + document.getElementById('toc-version').setAttribute('data-override-url-self', 'https://docs.typo3.org/other/t3docs/render-guides/main/en-us/Developer/InterlinkInventories.html'); |
| 111 | +
|
| 112 | +This must be performed before actually clicking the version switcher link, because |
| 113 | +the remote AJAX request is only performed once and then never again without reloading the page. |
0 commit comments