{"id":18561408,"url":"https://github.com/apostrophecms/apostrophe-astro","last_synced_at":"2025-04-10T03:30:46.121Z","repository":{"id":212688454,"uuid":"707362842","full_name":"apostrophecms/apostrophe-astro","owner":"apostrophecms","description":"An Astro integration to fetch content from ApostropheCMS. Add this module to the Astro application, not the Apostrophe application.","archived":false,"fork":false,"pushed_at":"2025-03-19T16:03:27.000Z","size":106,"stargazers_count":10,"open_issues_count":2,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-03T05:30:02.816Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apostrophecms.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-19T18:33:18.000Z","updated_at":"2025-03-19T15:56:55.000Z","dependencies_parsed_at":"2024-10-23T01:33:33.759Z","dependency_job_id":"7a1a30b1-0fd2-4132-8531-39f409317e15","html_url":"https://github.com/apostrophecms/apostrophe-astro","commit_stats":null,"previous_names":["apostrophecms/astro-integration","apostrophecms/apostrophe-astro"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fapostrophe-astro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fapostrophe-astro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fapostrophe-astro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fapostrophe-astro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apostrophecms","download_url":"https://codeload.github.com/apostrophecms/apostrophe-astro/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248150708,"owners_count":21055970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T22:06:46.107Z","updated_at":"2025-04-10T03:30:46.045Z","avatar_url":"https://github.com/apostrophecms.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/apostrophecms/apostrophe/main/logo.svg\" alt=\"ApostropheCMS logo\" width=\"80\" height=\"80\"\u003e\n\n  \u003ch1\u003e@apostrophecms/apostrophe-astro\u003c/h1\u003e\n  \u003cp\u003e\n    \u003ca aria-label=\"Apostrophe logo\" href=\"https://v3.docs.apostrophecms.org\"\u003e\n      \u003cimg src=\"https://img.shields.io/badge/MADE%20FOR%20ApostropheCMS-000000.svg?style=for-the-badge\u0026logo=Apostrophe\u0026labelColor=6516dd\"\u003e\n    \u003c/a\u003e\n    \u003ca aria-label=\"Join the community on Discord\" href=\"http://chat.apostrophecms.org\"\u003e\n      \u003cimg alt=\"\" src=\"https://img.shields.io/discord/517772094482677790?color=5865f2\u0026label=Join%20the%20Discord\u0026logo=discord\u0026logoColor=fff\u0026labelColor=000\u0026style=for-the-badge\u0026logoWidth=20\"\u003e\n    \u003c/a\u003e\n  \u003c/p\u003e\n\u003c/div\u003e\n\n# Astro integration for ApostropheCMS\n\nThis module integrates ApostropheCMS into your [Astro](https://astro.build/) application.\n\n## About Astro\n\nAstro provides a \"universal bridge\" to run modern frontend frameworks like React, Vue,\nand SvelteJS on the server side, as well as a straightforward, JSX-like template\nlanguage of its own to meld everything together.\n\n## Bringing ApostropheCMS and Astro together\n\nThe intent of this integration is to let Apostrophe manage content, handle routing of URLs and fetch content,\nand let Astro take the responsibility for the rendering of pages\nand any associated logic using your framework(s) of choice like React, Vue.js,\nSvelte, etc. (see the [Astro integrations page](https://docs.astro.build/en/guides/integrations-guide/) for more).\n\n**This module also brings the ApostropheCMS Admin UI in your Astro application**, so you can manage your site exactly as if you were in a \"normal\" Apostrophe instance.\n\nWhen you use this module, you will have **two** projects:\n\n1. An Astro project. This is where you write your templates and frontend code.\nAs a starting point, we recommend forking our\n[apostrophecms/astro-frontend](https://github.com/apostrophecms/astro-frontend) project.\n\n2. An Apostrophe project. This is where you define your page types, widget types\nand other content types with their schemas and other customizations. As a\nstarting point, we recommend forking our\n[apostrophecms/starter-kit-astro](https://github.com/apostrophecms/starter-kit-astro) project,\nor creating a new project from it using our CLI:\n\n```bash\napos create my-apos-project-name --starter=astro\n```\n\nThis kind of dual-project CMS integration is typical for Astro.\n\n\u003e Note that this module, `@apostrophecms/apostrophe-astro`, is meant to be installed as a dependency of the *Astro project*,\n\u003e not the Apostrophe project.\n\nThis module is currently designed for use with Astro's `output: 'server'` setting (SSR mode), so that you can edit your content\ndirectly on the page. Support for export as a static site is under consideration for the future.\n\n## Installation\n\nIf you did not fork the sample projects above, you will need to install this\nmodule into your Astro project. Install this module in your\n**Astro project**, not your ApostropheCMS project:\n\n```shell\ncd my-astro-project\nnpm install @apostrophecms/apostrophe-astro\n``` \n\n*Astro 3.x and 4.x are both supported.*\n\n## Security\n\nYou **must** set the `APOS_EXTERNAL_FRONT_KEY` environment variable to a secret\nvalue when running your Astro project, and also set the same variable to the same value when running your Apostrophe application.\nThis ensures that other sites on the web cannot fetch excessive amounts of\ninformation from ApostropheCMS without your permission.\n\n## Configuration (Astro)\n\nSince this is an Astro integration, you will need to add it to your Astro project's `astro.config.mjs` file.\nHere is a working `astro.config.js` file for a project with an Apostrophe CMS backend.\n\n```js\nimport { defineConfig } from 'astro/config';\nimport apostrophe from '@apostrophecms/apostrophe-astro';\n\n// For production. You can use other adapters that support\n// `output: 'server'`\nimport node from '@astrojs/node';\n\nexport default defineConfig({\n  output: 'server',\n  adapter: node({\n    mode: 'standalone'\n  }),\n  integrations: [\n    apostrophe({\n      aposHost: 'http://localhost:3000',\n      widgetsMapping: './src/widgets',\n      templatesMapping: './src/templates',\n      viewTransitionWorkaround: false,\n      includeResponseHeaders: [\n        'content-security-policy', \n        'strict-transport-security', \n        'x-frame-options',\n        'referrer-policy',\n        'cache-control'\n      ],\n      excludeRequestHeaders: [\n        // For single-site setups or hosting on multiple servers, block the host header\n        'host'\n      ]\n      proxyRoutes: [\n        // Custom URLs that should be proxied to Apostrophe.\n        // Note that all of `/api/v1` is already proxied, so\n        // this is usually unnecessary\n      ]\n    })\n  ],\n  vite: {\n    ssr: {\n      // Do not externalize the @apostrophecms/apostrophe-astro plugin, we need\n      // to be able to use virtual: URLs there\n      noExternal: [ '@apostrophecms/apostrophe-astro' ],\n    }\n  }\n});\n```\n\n## Options\n\n### `aposHost` (mandatory)  \n\nThis option is the base URL of your Apostrophe instance. It must contain the\nport number if testing locally and/or communicating directly with another instance\non the same server in a small production deployment. This option can be overriden\nat runtime with the `APOS_HOST` environment variable.\n\nDuring development it defaults automatically to: `http://localhost:3000`\n\n### `widgetsMapping` (mandatory)  \n\nThe file in your project that contains the mapping between Apostrophe widget types and your Astro components (see below).   \n\n### `templatesMapping` (mandatory)\n\nThe file in your project that contains the mapping between Apostrophe templates and your Astro templates (see below).\n\n### `viewTransitionWorkaround` (optional)\n\nIf set to `true`, Apostrophe will refresh its admin UI JavaScript on\nevery page transition, to ensure compatibility with Astro\n[view transitions](https://docs.astro.build/en/guides/view-transitions/).\nIf you are not using this feature of Astro, you can omit this flag to\nimprove performance for editors. Ordinary website visitors are\nnot impacted in any case. We are seeking an alternative solution to\neliminate this option.\n\n### `includeResponseHeaders`\n\nAn array of HTTP headers that you want to include from Apostrophe to the final response sent to the browser - useful if you want to use an Apostrophe module like `@apostrophecms/security-headers` and want to keep those headers as configured in Apostrophe and to preserve Apostrophe's caching headers.\n\nAt the present time, Astro is not compatible with the `nonce` property of `content-security-policy` `script-src` value. So this is automatically removed with that integration. The rest of the CSP header remains unchanged.\n\n### `excludeRequestHeaders`\n\nAn array of HTTP headers that you want to prevent from being forwarded from the browser to Apostrophe. This is particularly useful in single-site setups where you want to block the `host` header to allow Astro and Apostrophe to run on different hostnames.\n\nBy default, all headers are forwarded except those specified in this array.\n\n### `forwardHeaders` (deprecated)\n\nThis option has been replaced by `includeResponseHeaders` which provides clearer naming for its purpose. If both options are provided, `includeResponseHeaders` takes precedence. `forwardHeaders` will be removed in a future version.\n\n### Mapping Apostrophe templates to Astro components\n\nSince the front end of our project is entirely Astro, we'll need to create Astro components corresponding to each\ntemplate that Apostrophe would normally render with Nunjucks.\n\nCreate your template mapping in `src/templates/index.js` file.\nAs shown above, this file path must then be added to your `astro.config.mjs` file,\nin the `templatesMapping` option of the `apostrophe` integration.\n\n```js\n// src/templates/index.js\nimport HomePage from './HomePage.astro';\nimport DefaultPage from './DefaultPage.astro';\nimport BlogIndexPage from './BlogIndexPage.astro';\nimport BlogShowPage from './BlogShowPage.astro';\nimport NotFoundPage from './NotFoundPage.astro';\n\nconst templateComponents = {\n  '@apostrophecms/home-page': HomePage,\n  'default-page': DefaultPage,\n  '@apostrophecms/blog-page:index': BlogIndexPage,\n  '@apostrophecms/blog-page:show': BlogShowPage,\n  '@apostrophecms/page:notFound': NotFoundPage\n};\n\nexport default templateComponents;\n```\n\n#### How Apostrophe template names work\n\nFor ordinary page templates, like the home page or a typical \"default\" page type\nin an Apostrophe project, you can just specify the Apostrophe module name.\n\nFor special templates like `notFound`, and for modules that serve more than one\ntemplate, you'll need to specify the complete name. For instance, Apostrophe's\n`@apostrophecms/blog` module contains an `@apostrophecms/blog-page` page type\nthat renders an `index` template when viewing the main page of the blog, and\na `show` template when viewing a single blog post (a \"permalink\" page).\n\nIf you don't specify the template name, `:page` is assumed, which is just right\nfor ordinary page types.\n\nFor the \"404 Not Found\" page, use `@apostrophecms/page:notFound`, which is\nthe standard name for this template in ApostropheCMS.\n\n#### Special template names\n\nThe integration comes with two additional special template names that can be mapped to Astro templates.\nYou should not add a module name to these special names:\n\n- `apos-fetch-error`: served when Apostrophe generates a 500-class error. The integration will set Astro's response status to 500.\n- `apos-no-template`: served when there is no mapping corresponding to the Apostrophe page type for this page.\n\nSee below for an example Astro template for the `@apostrophe-cms/home-page` type. But first,\nlet's look at widgets.\n\n### Mapping Apostrophe widgets to Astro components\n\nSimilar to Astro page components, Astro widget components replace Apostrophe's usual\nwidget rendering.\n\nCreate your template mapping in a file in your application, for example in a\n`src/widgets/index.js` file. This file path must then be added to your `astro.config.mjs` file,\nin the `widgetsMapping` option of the `apostrophe` integration, as seen above.\n\n```js\n// src/widgets/index.js\n\nimport RichTextWidget from './RichTextWidget.astro';\nimport ImageWidget from './ImageWidget.astro';\nimport VideoWidget from './VideoWidget.astro';\nimport TwoColumnWidget from './TwoColumnWidget.astro';\n\nconst widgetComponents = {\n  // Standard widgets, but we must provide our own Astro components for them\n  '@apostrophecms/rich-text': RichTextWidget,\n  '@apostrophecms/image': ImageWidget,\n  '@apostrophecms/video': VideoWidget,\n  // Project-level widget\n  'two-column': TwoColumnWidget\n};\n\nexport default widgetComponents;\n```\n\n\u003e Note that even basic widget types like `@apostrophecms/image` do need an Astro\ntemplate in your project. This integration does not currently ship with built-in\nAstro templates for all of the common Apostrophe widgets. However, see the provided\n[astro frontend starter project](https://github.com/apostrophecms/astro-frontend) for examples of\nseveral of these.\n\nNote that the Apostrophe widget name (on the left) is the name of your widget module **without**\nthe `-widget` part.\n\nThe naming of your Astro widget templates is up to you. The above convention is just\na suggestion.\n\n### Creating the `[...slug.astro]` component and fetching Apostrophe data\n\nSince Apostrophe is responsible for managing URLs to content, including creating new content and pages\non the fly, you will only need one top-level Astro page component: the `[...slug].astro` route.\n\nThe integration comes with an `aposPageFetch` method that can be used to automatically\nfetch the relevant data for the current URL.\n\nYour `[...slug].astro` component should look like this:\n\n```js\n---\nimport aposPageFetch from '@apostrophecms/apostrophe-astro/lib/aposPageFetch.js';\nimport AposLayout from '@apostrophecms/apostrophe-astro/components/layouts/AposLayout.astro';\nimport AposTemplate from '@apostrophecms/apostrophe-astro/components/AposTemplate.astro';\n\nconst aposData = await aposPageFetch(Astro.request);\nconst bodyClass = `myclass`;\n\nif (aposData.redirect) {\n  return Astro.redirect(aposData.url, aposData.status);\n}\nif (aposData.notFound) {\n  Astro.response.status = 404;\n}\n---\n\u003cAposLayout title={aposData.page?.title} {aposData} {bodyClass}\u003e\n    \u003cFragment slot=\"standardHead\"\u003e\n      \u003cmeta name=\"description\" content={aposData.page?.seoDescription} /\u003e\n      \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /\u003e\n      \u003cmeta charset=\"UTF-8\" /\u003e\n    \u003c/Fragment\u003e\n    \u003cAposTemplate {aposData} slot=\"main\" /\u003e\n\u003c/AposLayout\u003e\n```\n\nThanks to the `aposPageFetch` call, the `aposData` object will then contain all of\nthe information normally provided by `data` in an ApostropheCMS Nunjucks template.\nThis includes, but is not limited to:\n\n- `page`: the page document for the current URL, if any\n- `piece`: the piece document when on a \"show page\" for a piece page type\n- `pieces`: an array of pieces when on an \"index page\" for a piece page type\n- `user`: information about the currently logged-in user\n- `global`: the ApostropheCMS global document e.g. global settings, editable global\nheaders and footers, etc.\n- `query`: the `req.query` object, giving access to query parameters in the URL.\n\nAny other data that your custom Apostrophe code attaches to `req.data` is also\navailable here.\n\n#### Understanding `AposLayout`\n\nThis integration comes with a full managed global layout, replacing the `outerLayout.html`\nused in Nunjucks page templates.\n\nIn your `[...slug].astro` file, use the `AposLayout` component built into this\nintegration to leverage the global layout.\n\nTo override any aspect of the global layout, take advantage of the following Astro slots,\nwhich are closely related to what ApostropheCMS offers in Nunjucks:\n\n- `startHead`: slot in the very beginning of the `\u003chead\u003e`\n- `standardHead`: slot in the middle of `\u003chead\u003e`, just after `\u003ctitle\u003e`  \n- `extraHead`: still in the HTML `\u003chead\u003e`, at the very end\n- `startBody`: at the very beginning of the `\u003cbody\u003e` - this is not part of the refresh zone in edit mode\n- `beforeMain`: at the very beginning of the main body zone - part of the refresh zone in edit mode\n- `main`: the inner part of the main body zone - part of the refresh zone in edit mode\n- `afterMain`: at the very end of the main body zone - part of the refresh zone in edit mode\n- `endBody`: at the very end of the `\u003cbody\u003e` - this is not part of the refresh zone in edit mode\n\nIn addition, the `AposLayout` component expects four props:\n\n- `aposData`: the data fetched from Apostrophe\n- `title`: this will go in the `\u003ctitle\u003e` HTML tag\n- `lang` which will be set in the `\u003chtml\u003e` `lang` attribute\n- `bodyClass`: this will be added in the `class` attribute of the `\u003cbody\u003e` element\n\nThis layout component will automatically manage the switch between support for\nthe editing UI if a user is logged in and a simpler \"Run Layout\" for all other\npage requests.\n\n#### Understanding `AposTemplate`\n\nThe role of `AposTemplate` is to automatically find the right Astro component\nto render based on the template mapping you created earlier. It accepts one\nprop, the full `aposData` object.\n\n### Creating Astro page components\n\nNext we'll look at how to write Astro page components, such as the\n`src/templates/HomePage.astro` file mentioned above.\n\n\u003e We do not recommend placing these in `src/pages` because their names are not\n\u003e routes and Astro should not try to compile them as routes. Place them in\n\u003e `src/templates` instead. `src/pages` should only contain the `[...slug.astro]` file.\n\nAs an example, let's take a look at a simple home page template:\n\n```js\n---\n// src/templates/HomePage.astro\nimport AposArea from '@apostrophecms/apostrophe-astro/components/AposArea.astro';\nconst { page } = Astro.props.aposData;\nconst { main } = page;\n---\n\n\u003csection\u003e\n  \u003ch1\u003e{ page.title }\u003c/h\u003e\n  \u003cAposArea area={main} /\u003e\n\u003c/section\u003e\n```\n\nNotice that we receive the `page` object from Apostrophe, which gives us\naccess to `page.title`. This is similar to `data.page` in a Nunjucks template.\n\n#### Understanding the `AposArea` component\n\nThis component allows Astro to render Apostrophe areas, and provides a\nstandard Apostrophe editing experience when doing so. Astro will automatically\ncall our widget components once content exists in the area. All we have to do is\npass on the area object, in this case the `main` schema field of `page`.\n\nNote that we can also pass area objects that are schema fields of widgets.\nThis allows for nested widgets, such as multiple-column widgets often used\nfor page layout.\n\nNote that additional props can be passed to the `AposArea` component and will be made\naccessible to widget components.\n\n### Creating Astro widget components\n\nEarlier we created a mapping from Apostrophe widget names to Astro components.\nLet's take a look at how to implement these.\n\nYou Astro widget will receive a `widget` property, in addition to any custom props\nyou passed to the `AposArea` component. This `widget` property contains the\nthe schema fields of your Apostrophe widget.  \n\nAs an example, here is a simple Astro component to render `@apostrophecms/image` widgets:\n\n```js\n---\nconst { widget } = Astro.props;\nconst placeholder = widget?.aposPlaceholder;\nconst src = placeholder ?\n  '/images/image-widget-placeholder.jpg' :\n  widget?._image[0]?.attachment?._urls['full'];\n---\n\u003cstyle\u003e\n  .img-widget {\n    width: 100%;\n  }\n\u003c/style\u003e\n\u003cimg class=\"img-widget\" {src} /\u003e\n```\n\n#### Placeholders are important in widgets that use them\n\nWhy are we checking for `aposPlaceholder`? Apostrophe's `@apostrophecms/image`\nwidget displays a placeholder image until the user clicks the edit pencil to\nselect their image of choice. When rendered by Astro, Apostrophe still expects\nthis to be the case. So we need to provide our own placeholder rendering.\n\nIn this case, a suitably named file must exist in `public/images` in our Astro project.\n\n#### Remember, relationship properties might not be populated\n\nIt is always possible that the image associated with an image widget has\nbeen archived. The `?.` syntax is a simple way to avoid a 500 error\nin such a situation. You may wish to add a more sophisticated fallback.\n\n### Accessing image and URLs\n\nProperties like `.attachment._urls['full']` exist on all image pieces,\nwhile properties like `.attachment._url` exist on non-image attachments\nsuch as PDFs. For more information, see\nthe [attachment field format](https://v3.docs.apostrophecms.org/reference/api/field-formats.html#attachment).\n\n## What to change in your Apostrophe project\n\nNothing! Well, almost.\n\n* Your project must be using Apostrophe 3.x.\n* You'll need to `npm update` your project to the latest version of `apostrophe`.\n* You'll need to set the `APOS_EXTERNAL_FRONT_KEY` environment variable to a secret\nvalue of your choosing when running Apostrphe.\n* Make sure you set that **same value** when running your Astro project.\n* To avoid developer confusion, we recommend changing any page templates in your\nApostrophe project to provide a link to your Astro frontend site and\nremove all other output. Everyone, editors included, should go straight to Astro.\n\n## Starting up your combined project\n\nTo start your Astro project, follow the usual practice:\n\n```bash\ncd my-astro-project\nnpm install\nexport APOS_EXTERNAL_FRONT_KEY=your-secret-goes-here\nnpm run dev\n```\n\nIn an adjacent terminal, start your Apostrophe project:\n\n```bash\ncd my-apostrophe-project\nnpm install\nexport APOS_EXTERNAL_FRONT_KEY=your-secret-goes-here\nnpm run dev\n```\n\nFor convenience, Astro generally defaults to port `4321`, while\nApostrophe defaults to port `3000`.\n\n## Logging in\n\nOnce your integration is complete, you will be able to reach the login page in\nthe usual way at `http://localhost:4321/login`. Astro proxies this route directly\nto Apostrophe. Therefore any additional extensions you have added such as\nApostrophe's hCaptcha and TOTP modules will work as expected.\n\n## Redirections\nWhen Apostrophe sends a response as a redirection, you will receive a specially\nformatted `aposData` object containing `redirect: true`, a `url` property for the url\nto redirect to, and a `status` for the redirection HTTP status code. This is handled\nin the earlier example, repeated here for convenience:\n\n```js\nconst aposData = await aposPageFetch(Astro.request)\n// Redirect\nif (aposData.redirect) {\n  return Astro.redirect(aposData.url, aposData.status);\n}\n```\n\n## 404 Not Found\nMuch like the redirect case, when Apostrophe determines that the page was not\nfound, `aposData.notFound` will be set to true. The example `[...slug].astro`\nfile provided above includes logic to set Astro's status code to 404 in this\nsituation.\n\n## Reserved routes\nAs this integration proxies certain Apostrophe endpoints, there are some routes that are taken by those endpoints:   \n- `/apos-frontend/[...slug]` for serving Apostrophe assets\n- `/uploads/[...slug]` for serving Apostrophe uploaded assets\n- `/api/v1/[...slug]` and `/[locale]/api/v1/[...slug]` for Apostrophe API endpoints\n- `/login` and `/[locale]/login` for the login page\n\nAs all Apostrophe API endpoints are proxied, you can expose new api routes as usual in your Apostrophe modules, and be able to request them through your Astro application.   \nThose proxies are forwarding all of the original request headers, such as cookies, so that Apostrophe login works normally.\n\n## What about widget players?\n\nApostropheCMS is very unopinionated on the front end, but it does include one\nimportant front end feature: widget players. These provide a way for developers\nto provide special behavior to widgets, calling each widget's player exactly\nonce at page load and when new widgets are inserted or replaced with new values.\nUsers appreciate this and expect interactive widget features to work normally\nwithout a page refresh, even if the widget was just added to the page.\n\nIn Astro, web components are a recommended strategy to achieve the same thing.\nDefining and using a web component in an Astro widget component has much\nthe same effect as defining a widget player in a standalone Apostrophe project.\n\nHere is a simple outline of such a web component. For a complete example of\nthe same widget, check out the source code of `VideoWidget.astro` in our\n[apostrophecms/astro-frontend](https://github.com/apostrophecms/astro-frontend) project.\n\n```js\n---\n// src/widgets/VideoWidget.astro\nconst { widget } = Astro.props;\nconst placeholder = widget?.aposPlaceholder ? 'true' : '';\nconst url = widget?.video?.url;\n---\n\u003cstyle\u003e\n  video-widget {\n    width: 100%;\n  }\n\u003c/style\u003e\n\u003cvideo-widget\n  url={placeholder ? 'https://youtu.be/Q5UX9yexEyM' : url }\n\u003e\n\u003c/video-widget\u003e\n\u003cscript\u003e\n  class VideoWidget extends HTMLElement {\n    constructor() {\n      super();\n      this.init();\n    }\n    async init() {\n      const videoUrl = this.getAttribute('url');\n      // Your logic here!\n      //\n      // Fetch details about the video URL,\n      // create an iframe to embed it, append it\n      // to the component's HTML element with this.append(),\n      // etc.\n    }\n  }\n  customElements.define('video-widget', VideoWidget);\n\u003c/script\u003e\n```\n\n\u003e Note that Astro script tags aren't really plain vanilla HTML script tags.\n\u003e They are efficiently compiled, support TypeScript and are only executed\n\u003e once even if the component appears may times on the page. Defining a\n\u003e web component allows us to leverage that code more than once by using\n\u003e the newly defined element as often as we wish.\n\n## `aposSetQueryParameter`: working with query parameters\n\nOne last thing: query parameters. Sometimes we want to create pagination\nlinks with page numbers, add filters to a URL's query string, and so on.\nBut, working with query parameters coming from Apostrophe can\nbe a little bit tricky because there are often special query parameters\npresent during editing that should not be part of a visible URL.\n\nAs a convenience, Apostrophe provides `aposSetQueryParameter` to abstract\nall that away.\n\nHere is how the `BlogIndexPage.astro` component of the\n[apostrophecms/astro-frontend](https://github.com/apostrophecms/astro-frontend) project generates\nlinks to the each page of blog posts:\n\n```js\n---\nimport setParameter from '@apostrophecms/apostrophe-astro/lib/aposSetQueryParameter.js';\n\nconst {\n  pieces,\n  currentPage,\n  totalPages\n} = Astro.props.aposData;\n\nconst pages = [];\nfor (let i = 1; (i \u003c= totalPages); i++) {\n  pages.push({\n    number: i,\n    current: page === currentPage,\n    url: setParameter(Astro.url, 'page', i)\n  });\n}\n---\n\n\u003csection class=\"bp-content\"\u003e\n  \u003ch1\u003e{ page.title }\u003c/h1\u003e\n\n  \u003ch2\u003eBlog Posts\u003c/h2\u003e\n\n  {pieces.map(piece =\u003e (\n    \u003ch3\u003e\n      \u003ca href={ piece._url }\u003e{ piece.title }\u003c/a\u003e\n    \u003c/h3\u003e\n  ))}\n\n  {pages.map(page =\u003e (\n    \u003ca\n      class={(page === currentPage) ? 'current' : ''} \n      href={page.url}\u003e{page.number}\n    \u003c/a\u003e\n  ))}\n\u003c/section\u003e\n```\n\nImported here as `setParameter`, `aposSetQueryParameter` allows\nus to do two things:\n\n1. Take a URL and return a new URL with a certain query parameter set\nto a new value.\n2. Remove a query parameter completely by passing the empty string as\na value, or by passing `null` or `undefined`.\n\nWhile you can get the same result by manipulating `Astro.url` yourself,\nyou'll be able to avoid the confusing presence of query parameters\nlike `aposMode` by using this convenient feature.\n\n## What about Vue, React, SvelteJS, etc.?\n\nWhile not shown directly in the examples above, **Astro can import components\nwritten in any of these frameworks.** Just use `astro add` to install\nthe appropriate integration, then `import` your components freely in your\n`.astro` files. For complete documentation and examples, see the\n[`@astrojs/react` integration](https://docs.astro.build/en/guides/integrations-guide/react/).\n\nIn this way, Astro acts as a **universal bridge** to essentially all modern\nfrontend frameworks.\n\n## A note on production use\n\nFor production use, any Astro hosting adapter that supports `mode: 'server'` should\nbe acceptable. In particular, our [apostrophecms/astro-frontend](https://github.com/apostrophecms/astro-frontend) project comes pre-configured\nfor the `node` adapter, and includes `npm run build` and `npm run serve`\nsupport to take advantage of that. In `server` mode there is not a great\ndeal of difference between these and `npm run dev`, but there is less\noverhead and less information exposed to the public, so we recommend following\nthis best practice.\n\n## Debugging\n\nIn most cases, Astro prints helpful error messages directly in the browser\nwhen in a development environment.\n\nHowever, if you receive the following error:\n\n```\nOnly URLs with a scheme in: file and data are supported by the default ESM\nloader. Received protocol 'virtual:'\n```\n\nThen you most likely left out this part of the above `Astro.config.js` file:\n\n```javascript\nexport default defineConfig({\n  // ... other settings above here ...\n  vite: {\n    ssr: {\n      // Do not externalize the @apostrophecms/apostrophe-astro plugin, we need\n      // to be able to use virtual: URLs there\n      noExternal: [ '@apostrophecms/apostrophe-astro' ],\n    }\n  }\n});\n```\n\nWithout this logic, the `virtual:` URLs used to access configuration information\nwill cause the build to fail.\n\n## Conclusion\n\nThis module provides a new way to use ApostropheCMS: as a back end\nfor modern front end development in Astro. But more than that, it\nprovides a future-proof bridge to many different front-end frameworks.\n\nAlso important, Apostrophe fully maintains the on-page, in-context editing\nexperience when integrated with Astro, going beyond \"side-by-side\"\nediting experiences to achieve integration close enough that we often\nhave to look at the address bar to know whether we are looking at\nAstro or Apostrophe.\n\nThat being said, this integration is also new, and we encourage you\nto share your feedback.\n\n## Acknowledgements\n\nDevelopment of this module began with Stéphane Maccari and Clément Ravier of\nMichelin. We are grateful for their generous support of ApostropheCMS.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapostrophecms%2Fapostrophe-astro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapostrophecms%2Fapostrophe-astro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapostrophecms%2Fapostrophe-astro/lists"}