Skip to Content
Vuesho

Feedback

Collect feedback on your pages.

@vuesho/feedback adds a form under each page, where readers rate the page and leave a message, and a popup to comment on selected text. The feedback goes to a handler on your server, to GitHub Discussions or to email.

Setup

Install the Package

npm i @vuesho/feedback

Choose Where Feedback Goes

Each plugin sends both kinds of feedback to one place:

feedbackPlugin() runs your handlers on the server, and each form shows only when its handler is set. When a handler returns a githubUrl, the form shows a link to it after the reader sends the form:

vuesho.config.ts
import { feedbackPlugin } from "@vuesho/feedback";
import { createSite, recommendedPreset } from "vuesho/config";
import { createDiscussion, track } from "./server/feedback";

export default createSite(docs.toSource()).plugins(
  recommendedPreset(),
  feedbackPlugin({
    async onPageFeedbackAction({ opinion, url, message }) {
      await track("page-feedback", { opinion, url, message });
      return {};
    },
    async onTextFeedbackAction({ url, blockId, blockBody, message }) {
      const discussion = await createDiscussion({ url, blockId, blockBody, message });
      return { githubUrl: discussion.url };
    },
  }),
);

A handler can throw a Response to send it as the answer, like a 400 that rejects a message.

Deploy With a Server

The forms send to the API routes /api/feedback/page and /api/feedback/text, which need a server, so a static build fails. On a static site, send the feedback from the browser instead.

.env.local isn't deployed, so set the keys in your host's environment variables. Without them, the build succeeds, but each request fails with an error that names the missing key.

Rate Limiting

The routes are public, and don't limit the length of a message. Add rate limiting and bot protection before the site goes live.

A plugin's middleware can reject requests before they reach the routes:

vuesho.config.ts
import { isRateLimited } from "./server/rate-limit"; 

createSite(docs.toSource()).plugins(
  recommendedPreset(),
  feedbackPlugin({ onPageFeedbackAction }),
  {
    name: "site:rate-limit",
    middleware: {
      async handle(request, next, { page }) {
        if (page.startsWith("/api/feedback/") && (await isRateLimited(request)))
          return new Response("Too Many Requests", { status: 429 });
        return next();
      },
    },
  },
);

Static Sites

Feedback and FeedbackText from @vuesho/feedback/ui are the forms. Their action prop takes a URL or a function, so a static site can send the feedback to another service from the browser:

src/components/PageFeedback.vue
<script setup lang="ts">
import { Feedback, type ActionResponse, type PageFeedback } from "@vuesho/feedback/ui";

async function send(feedback: PageFeedback): Promise<ActionResponse> {
  await fetch("https://feedback.acme.dev", { method: "POST", body: JSON.stringify(feedback) });
  return {};
}
</script>

<template>
  <Feedback :action="send" />
</template>

Put the form in the page-bottom slot of your theme, without a feedback plugin:

src/theme.ts
import { defineAsyncComponent } from "vue";
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";

export default defineTheme({
  extends: defaultTheme,
  slots: {
    "page-bottom": defineAsyncComponent(() => import("./components/PageFeedback.vue")),
  },
});

Put FeedbackText in the page-content slot, around the content, and add blockId from vuesho/markdown, so its popup opens on each block:

vuesho.config.ts
import { blockId } from "vuesho/markdown"; 

createSite(docs.toSource()).config({
  markdown: { markdownItPlugins: [[blockId, { addDataAttribute: "feedback" }]] }, 
});

To translate the forms, also add feedbackPlugin() without handlers. It adds the forms' translations, without forms or routes.

Customizing the UI

The page form shows in the page-bottom slot, and the text popup wraps page-content. To place the forms yourself and keep the routes, replace the plugin's theme with your own theme module:

vuesho.config.ts
createSite(docs.toSource()).plugins(
  recommendedPreset(),
  feedbackPlugin({ onPageFeedbackAction }), 
  {
    ...feedbackPlugin({ onPageFeedbackAction }),
    theme: "/src/feedback-theme.ts",
  },
);

Your theme module puts the form in another slot, like toc-footer under the table of contents:

src/feedback-theme.ts
import { defineAsyncComponent } from "vue";
import type { PluginTheme } from "vuesho/client";

export default {
  slots: {
    "toc-footer": defineAsyncComponent(() => import("./components/PageFeedback.vue")),
  },
} satisfies PluginTheme;

page.pageData.feedback holds the routes' URLs. Pass one to the form as its action:

src/components/PageFeedback.vue
<script setup lang="ts">
import { computed } from "vue";
import { Feedback } from "@vuesho/feedback/ui";
import { usePage } from "vuesho/client";
import type app from "../../vuesho.config";

const page = usePage<typeof app>();
const action = computed(() =>
  page.value.kind === "content" ? page.value.pageData.feedback?.page : undefined,
);
</script>

<template>
  <Feedback v-if="action" :action="action" />
</template>

Wrapping Each Block

To show a feedback button beside each paragraph instead, wrap each block that has an ID in a component, with a markdown-it rule and createVueElement() from vuesho/markdown, as in Markdown Extensions:

Translations

The forms have language packs for zh-CN and zh-TW. For another language, or to change a label, set ui.translations, as in Internationalization:

vuesho.config.ts
createSite(docs.toSource(), { i18n }).config({
  ui: {
    translations: { fr: { "How is this guide?(feedback)": "Ce guide vous a-t-il aidé ?" } }, 
  },
});

Options

feedbackPlugin()

Prop

Type

githubFeedbackPlugin()

Prop

Type

emailFeedbackPlugin()

Prop

Type

What a Handler Receives

Prop

Type

Prop

Type