-
-
Notifications
You must be signed in to change notification settings - Fork 372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
creating a shiki wrapper for nextjs, and rsc #731
Comments
I've been experimenting with shiki and next.js with app router for the last few days. So far I have been unable to get SSR working for dynamic pages. Only static build or client side highlighting works. When creating a highlighter at runtime, it fails to dynamically import the languages. (From other issues, it seems next.js might not support the kind of import shiki is doing) I've tried to bypass that by using the core highlighter and playing with the imports, but no luck so far. Would this wrapper/enhancement help with that use case? Otherwise next.js seems to work for me on client side / static site generation. |
yeah
yeah i have been working on it , it could work even now with ssr for dynamic pages . i have created this wrapper , but i am still working on it ,
Examplei am also working on dynamic languages import without loosing optimizations
also see this article must read last lines , final thoughts |
Sounds promising, but could be hard to maintain the mapping on top of shiki. I found that importing from import {createHighlighterCore} from "@shikijs/core";
import getWasm from "@shikijs/core/wasm-inlined";
async function getHighlighter (){
const highlighter = await createHighlighterCore({loadWasm: getWasm});
highlighter.loadLanguage(/* can't use import("shiki/langs/javascript.mjs") */);
return highlighter;
} |
i am not doing any mapping on it , the mappings are already in the shiki package , the are using it |
That approach works locally with NextJS14 Server components, but not on Vercel. import {
transformerNotationHighlight,
transformerNotationWordHighlight,
} from '@shikijs/transformers';
import { type HighlighterCore } from 'shiki';
import { createHighlighterCore } from 'shiki/core';
import tsLang from 'shiki/langs/typescript.mjs';
import githubTheme from 'shiki/themes/github-dark.mjs';
import getWasm from 'shiki/wasm';
export const createHighlighter = async () =>
await createHighlighterCore({
themes: [githubTheme],
langs: [tsLang],
loadWasm: getWasm,
});
export let highlighter: HighlighterCore
export const highlight = async (content: string) => {
if (!highlighter) {
highlighter = await createHighlighter()
}
return highlighter?.codeToHtml?.(content, {
lang: 'typescript',
theme: 'github-dark',
transformers: [
transformerNotationHighlight(),
transformerNotationWordHighlight(),
],
}) || '';
} and use like this import { highlight } from '../highlighter';
export const File = async ({
content,
}: {
content: string;
}) => {
const text = await highlight(content);
return (
<code>
<pre
dangerouslySetInnerHTML={{ __html: text }}
className=" dark:bg-gray-800 p-4"
></pre>
</code>
);
}; @fuma-nama need to use "Fine-grained bundle", with edge function bundle size is very limited, from docs couldn't find a proper way to do it with Next.js. |
Production build works as expected: https://shiki-test-eight.vercel.app
You're not supposed to use Shiki in edge runtime, it uses Node.js APIs. And honestly, you have no reasons to use Shiki under edge environments like Middleware, for normal pages, serverless is absolutely enough |
Ok, better to add more exhaustive docs for Next.js, still source of confusion. |
Hmm I see, maybe open a separate issue? Would be nice to have a Next.js guide under the integrations section. At least from what I see, the original issue of OP has been solved on newer versions of Next.js. |
|
For Shiki itself, we don't have a plan to maintain wrappers for each specific metaframeworks. We'll leave that to community solutions. Marking this as not planned to make it clear. Thanks for bringing it up. |
Clear and concise description of the problem
The Shiki is Vue and Nuxt first , it also works fine with the next js , but there are a few problems with it when using in nextjs. it just does not support it the way it works with vue. and we have too import different functions and each have their own set of rules.
Suggested solution
Therefore , I am working on a shiki nextjs, react , rsc wrapper , i am following the best practices, using type guards, and i want to know what suggestions community will give. i will also write the docs (in nextjs with turborepo,fuma),
i am following a pattern of single config. you only have to create a function generateShiki.
actions have the actions like 'codeToHtml' | 'codeToHast' | 'codeToTokens' | 'codeToTokensBase' | 'codeToTokensWithThemes';
etc
there are some base options , in each action we can overide the default options , and each action have it's appropriate options.
to use a theme in the base options we have to load it in the custom highlight options,
actions is an array on action , if we remove an action and invoke their function will be undefined.
we do not have to write that much to get started , it is an example so i did it intentionally to explain the structure.
we can start with just writting
Validations
Contributes
The text was updated successfully, but these errors were encountered: