Skip to content
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

feat(next): added new import for nextjs with ssr disabled #48

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
363 changes: 346 additions & 17 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions packages/react-uploader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ React principles.
* [Styles](#styles)
* [File Uploader API](#file-uploader-api)
* [Events](#events)
* [Next.js](#nextjs)
* [Security issues](#security-issues)
* [Feedback](#feedback)

Expand Down Expand Up @@ -120,8 +121,7 @@ import {
import "@uploadcare/react-uploader/core.css";

const Example = () => {
const uploaderRef = useRef < InstanceType < UploadCtxProvider > | null > (null);

const uploaderRef = useRef <InstanceType<UploadCtxProvider> | null>(null);

<FileUploaderRegular apiRef={uploaderRef} pubkey="YOUR_PUBLIC_KEY"/>;
}
Expand Down Expand Up @@ -172,6 +172,21 @@ import "@uploadcare/react-uploader/core.css";
| change | onChange |
| group-created | onGroupCreated |

## Next.js
File Uploader does not support Server-side Rendering (SSR), we have a special import for nextjs that already has SSR disabled.
You will need to import with import `@uploadcare/react-uploader/next`

```jsx
'use client'
import { FileUploaderRegular } from "@uploadcare/react-uploader/next";
import "@uploadcare/react-uploader/core.css";

function App() {
return <FileUploaderRegular pubkey="YOUR_PUBLIC_KEY" />
};
```


## Security issues

If you think you ran into something in Uploadcare libraries that might have
Expand Down
14 changes: 9 additions & 5 deletions packages/react-uploader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"version": "1.0.0",
"private": false,
"type": "module",
"files": [
"dist"
],
"files": ["dist"],
"main": "./dist/react-uploader.cjs",
"module": "./dist/react-uploader.js",
"types": "./dist/react-uploader.d.ts",
Expand All @@ -15,7 +13,12 @@
"import": "./dist/react-uploader.js",
"require": "./dist/react-uploader.cjs"
},
"./core.css": "./dist/libs.css"
"./next": {
"import": "./dist/nextjs.js",
"require": "./dist/nextjs.cjs",
"types": "./dist/nextjs.d.ts"
},
"./core.css": "./dist/style.css"
},
"scripts": {
"dev": "tsc && vite build --watch",
Expand All @@ -24,7 +27,8 @@
"test": "vitest"
},
"peerDependencies": {
"@types/react": "17 || 18"
"@types/react": "17 || 18",
"next": "13 || 14"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to mark next dependency as optional peer: https://docs.npmjs.com/cli/v7/configuring-npm/package-json#peerdependenciesmeta

},
"dependencies": {
"@uploadcare/file-uploader": "^1.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dynamic from "next/dynamic";

export const FileUploaderInline = dynamic(
() => import("./FileUploaderInline").then((mod) => mod.FileUploaderInline),
{ ssr: false },
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dynamic from "next/dynamic";

export const FileUploaderMinimal = dynamic(
() => import("./FileUploaderMinimal").then((mod) => mod.FileUploaderMinimal),
{ ssr: false },
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dynamic from "next/dynamic";

export const FileUploaderRegular = dynamic(
() => import("./FileUploaderRegular").then((mod) => mod.FileUploaderRegular),
{ ssr: false },
);
3 changes: 3 additions & 0 deletions packages/react-uploader/src/nextjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { FileUploaderMinimal } from "./Uploader/Minimal/NextFileUploaderMinimal";
export { FileUploaderRegular } from "./Uploader/Regular/NextFileUploaderRegular";
export { FileUploaderInline } from "./Uploader/Inline/NextFileUploaderInline";
13 changes: 8 additions & 5 deletions packages/react-uploader/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import dts from "vite-plugin-dts";

export default defineConfig({
build: {
cssCodeSplit: true,
cssCodeSplit: false,
lib: {
entry: [resolve(__dirname, "src/libs.ts")],
entry: {
nextjs: resolve(__dirname, "src/nextjs.ts"),
"react-uploader": resolve(__dirname, "src/libs.ts"),
},

name: "@uploadcare/react-uploader",

formats: ["es", "cjs"],

fileName: "react-uploader",
},
rollupOptions: {
external: ["react", "@uploadcare/file-uploader"],
external: ["react", "next", "next/dynamic", "@uploadcare/file-uploader"],
output: {
globals: {
react: "React",
next: "next",
},
},
},
Expand Down