0.1.89 • Published 5 months ago

react-edge v0.1.89

Weekly downloads
-
License
MIT*
Repository
github
Last release
5 months ago

React Edge Framework 🚀

A revolutionary React framework for building blazing-fast applications on the edge, powered by Cloudflare Workers

TypeScript Vitest MIT License

🚀 Motivation

Read the full story on DEV.to

🌟 Features

  • Powerful Data Fetching & RPC: Write server-side methods and use them with an incredibly flexible fetching system:
// Define your types
type User = {
  id: string;
  name: string;
  preferences: {
    theme: 'light' | 'dark';
    notifications: boolean;
  };
};

type Product = {
  id: string;
  name: string;
  price: number;
  category: string;
};

// Server: Your API methods
class API extends Rpc {
  async getUser(id: string): Promise<User> {
    return this.db.users.findById(id);
  }

  async getProducts(category: string): Promise<Product[]> {
    return this.db.products.findByCategory(category);
  }

  async searchProducts(query: string): Promise<Product[]> {
    return this.db.products.search(query);
  }
}

// Client: Unleash the power of useFetch!
const Dashboard = () => {
  const { rpc } = app.useContext<App.Context>();
  const [searchQuery, setSearchQuery] = useState('');
  const [selectedCategory, setSelectedCategory] = useState('all');

  // 🚀 Basic fetch with automatic SSR and hydration
  const { data: user } = app.useFetch(async (ctx, id) => {
	return ctx.rpc.getUser(id ?? '123');
  });

  // 🚀 Reactive fetch with dependencies
  const { data: products, loading, error, fetch: refreshProducts } = app.useFetch(
    async ctx => ctx.rpc.getProducts(selectedCategory),
    {
      // Refetch when category changes
      deps: [selectedCategory]
    }
  );

  // 🚀 Debounced search with automatic error handling
  const { data: searchResults } = app.useFetch(
    async ctx => ctx.rpc.searchProducts(searchQuery),
    {
      // Only fetch if we have a query
      shouldFetch: () => searchQuery.length > 2,
	  deps: [searchQuery],
      // Debounce the API calls
      depsDebounce: 300
    }
  );

  // 🚀 Parallel fetching with batch
  const { data: dashboardData } = app.useFetch(
    async ctx => {
      const [
        userProfile,
        recentProducts,
        recommendations
      ] = await ctx.rpc.batch([
        ctx.rpc.getUser('123'),
        ctx.rpc.getProducts('recent'),
        ctx.rpc.searchProducts('recommended')
      ]);

      return {
        profile: userProfile,
        recent: recentProducts,
        recommended: recommendations
      };
    },
    {
      // Automatic polling every 30 seconds
      interval: 30000
    }
  );

  return (
    <div>
      {/* All your data is fully typed! */}
      <UserProfile user={user} />

      <ProductGrid
        products={products}
        loading={loading}
        onRefresh={refreshProducts}
      />

      <SearchResults results={searchResults} />

      <DashboardWidgets data={dashboardData} />
    </div>
  );
};

🎯 useFetch Features:

  • Seamless SSR & Hydration: Data is preloaded during SSR and hydrated on the client automatically
  • Dependency-based Refetching: Reactive updates when dependencies change
  • Smart Debouncing: Control the frequency of API calls with depsDebounce
  • Intelligent Batching: Multiple RPC calls are automatically combined into a single request
  • Simple Polling: Easy periodic data updates with interval
  • Type Safety: Full TypeScript support with zero manual type declarations
  • Conditional Fetching: Control when fetching should occur with shouldFetch
  • Manual Controls: Programmatic refresh capabilities with fetch function
  • Performance Optimized: Automatic request deduplication and caching

📦 Installation

npm install react-edge

🚀 Quick Start

1. Set up your Worker

// worker.ts
const handler = {
	fetch: async (req: Request, env: types.Worker.Env, context: ExecutionContext) => {
		const workerApp = new AppWorkerEntry({
			i18n: {
				en: await import('./translations/en'),
				es: await import('./translations/es')
			}
		});

		return await workerApp.fetch();
	}
};

2. Create your first RPC endpoint

class UserAPI extends Rpc {
	async getProfile(id: string) {
		const user = await this.db.users.findById(id);

		return this.createResponse(user, {
			cache: {
				tags: [`user:${id}`],
				ttlSeconds: 300 // Cache for 5 minutes
			}
		});
	}
}

3. Use it in your components

const UserProfile = () => {
  const { rpc } = app.useContext<App.Context>();

  const { data: profile, loading } = app.useFetch(
    async ctx => ctx.rpc.getProfile('123')
  );

  if (loading) return <Loading />;

  return <div>Welcome, {profile.name}!</div>;
};

🛠 Core Features

Typed RPC System

The RPC system enables seamless client-server communication with full TypeScript support:

class ProductsAPI extends Rpc {
	// Private methods (not exposed to client)
	private async _validateProduct(data: ProductData) {
		// or $validateProduct , or #validateProduct
		return productSchema.parse(data);
	}

	// Public methods (accessible via RPC)
	async createProduct(data: ProductData) {
		const validated = await this._validateProduct(data);
		return this.db.products.create(validated);
	}
}

Smart Caching

Built-in caching system with tag-based invalidation:

class CacheExample extends Rpc {
	async getProducts(category: string) {
		const products = await this.db.products.findByCategory(category);

		return this.createResponse(products, {
			cache: {
				tags: [`category:${category}`, 'products'],
				ttlSeconds: 3600 // 1 hour
			}
		});
	}

	async updateProduct(id: string, data: ProductData) {
		await this.db.products.update(id, data);

		// Invalidate specific caches
		await this.cache.deleteBy({
			tags: [`product:${id}`, `category:${data.category}`]
		});
	}
}

Authentication

Simple JWT authentication with secure cookie management:

class AuthAPI extends Rpc {
	private auth = new AuthJwt({
		cookie: 'token',
		encrypt: true,
		expires: { days: 1 },
		secret: process.env.JWT_SECRET
	});

	async login(credentials: LoginData) {
		const { headers } = await this.auth.sign(credentials);
		return this.createResponse({ success: true }, { headers });
	}
}

State Management

URL-Synced State

const FiltersComponent = () => {
  const [filters, setFilters] = app.useUrlState({
    category: 'all',
    minPrice: 0,
    maxPrice: 1000
  }, {
    debounce: 500,
    kebabCase: true
  });

  return <FilterPanel value={filters} onChange={setFilters} />;
};

Persistent State

const UserPreferences = () => {
  const [prefs, setPrefs] = app.useStorageState('user-prefs', {
    theme: 'light',
    fontSize: 16
  }, {
    storage: 'local',
    debounce: 300
  });

  return <PreferencesPanel value={prefs} onChange={setPrefs} />;
};

🛠 CLI Commands

React Edge comes with a powerful CLI for development and deployment:

Development

# Start development server
yarn edge dev

# Start development server for app only
yarn edge dev --app

# Start development server for worker only
yarn edge dev --worker

Build

# Build entire project
yarn edge build

# Build for specific environment
yarn edge build --env production

# Build app or worker separately
yarn edge build --app
yarn edge build --worker

Other Commands

# Deploy to Cloudflare Workers
yarn edge deploy

# View worker logs
yarn edge logs

# Run linting
yarn edge lint

# Run tests
yarn edge test

# Type checking
yarn edge type-check

🌍 Internationalization

Easy i18n support with variable interpolation:

// translations/fr.ts
export default {
  'Welcome, {name}!': 'Bienvenue, {name}!'
};

// Component
const Welcome = () => (
  <h1>{__('Welcome, {name}!', { name: 'John' })}</h1>
);

📚 Best Practices

  1. RPC Organization

    • Keep related functionality in dedicated RPC classes
    • Use private methods for internal logic
    • Leverage caching for frequently accessed data
  2. State Management

    • Use useUrlState for shareable state
    • Use useStorageState for persistent preferences
    • Use useDistinct to optimize updates
  3. Performance

    • Implement appropriate cache strategies
    • Use Link component for preloading
    • Leverage edge caching when possible

📝 License

MIT © Felipe Rohde

👨‍💻 Author

Felipe Rohde

0.1.52

8 months ago

0.1.53

8 months ago

0.1.54

8 months ago

0.1.55

8 months ago

0.1.56

8 months ago

0.1.57

8 months ago

0.1.58

8 months ago

0.1.59

8 months ago

0.1.50

8 months ago

0.1.51

8 months ago

0.1.118

5 months ago

0.1.117

5 months ago

0.1.119

5 months ago

0.1.114

5 months ago

0.1.113

5 months ago

0.1.116

5 months ago

0.1.115

5 months ago

0.1.110

5 months ago

0.1.49

8 months ago

0.1.112

5 months ago

0.1.111

5 months ago

0.1.41

8 months ago

0.1.42

8 months ago

0.1.43

8 months ago

0.1.44

8 months ago

0.1.45

8 months ago

0.1.46

8 months ago

0.1.47

8 months ago

0.1.48

8 months ago

0.1.40

8 months ago

0.1.129

5 months ago

0.1.128

5 months ago

0.1.125

5 months ago

0.1.124

5 months ago

0.1.127

5 months ago

0.1.126

5 months ago

0.1.121

5 months ago

0.1.38

8 months ago

0.1.120

5 months ago

0.1.39

8 months ago

0.1.123

5 months ago

0.1.122

5 months ago

0.1.30

9 months ago

0.1.31

9 months ago

0.1.32

9 months ago

0.1.33

9 months ago

0.1.34

8 months ago

0.1.35

8 months ago

0.1.36

8 months ago

0.1.37

8 months ago

0.1.139

4 months ago

0.1.136

4 months ago

0.1.135

4 months ago

0.1.138

4 months ago

0.1.137

4 months ago

0.1.132

5 months ago

0.1.27

9 months ago

0.1.131

5 months ago

0.1.28

9 months ago

0.1.134

4 months ago

0.1.29

9 months ago

0.1.133

4 months ago

0.1.130

5 months ago

0.1.20

9 months ago

0.1.21

9 months ago

0.1.22

9 months ago

0.1.23

9 months ago

0.1.24

9 months ago

0.1.25

9 months ago

0.1.26

9 months ago

0.1.147

4 months ago

0.1.146

4 months ago

0.1.149

4 months ago

0.1.148

4 months ago

0.1.143

4 months ago

0.1.142

4 months ago

0.1.145

4 months ago

0.1.144

4 months ago

0.1.141

4 months ago

0.1.140

4 months ago

0.1.99

5 months ago

0.1.90

5 months ago

0.1.91

5 months ago

0.1.92

5 months ago

0.1.93

5 months ago

0.1.94

5 months ago

0.1.95

5 months ago

0.1.158

4 months ago

0.1.157

4 months ago

0.1.159

4 months ago

0.1.154

4 months ago

0.1.153

4 months ago

0.1.156

4 months ago

0.1.155

4 months ago

0.1.150

4 months ago

0.1.152

4 months ago

0.1.151

4 months ago

0.1.85

6 months ago

0.1.86

6 months ago

0.1.87

5 months ago

0.1.88

5 months ago

0.1.89

5 months ago

0.1.80

6 months ago

0.1.81

6 months ago

0.1.82

6 months ago

0.1.83

6 months ago

0.1.84

6 months ago

0.1.161

4 months ago

0.1.160

4 months ago

0.1.163

4 months ago

0.1.162

4 months ago

0.1.74

6 months ago

0.1.75

6 months ago

0.1.76

6 months ago

0.1.77

6 months ago

0.1.78

6 months ago

0.1.79

6 months ago

0.1.70

7 months ago

0.1.71

7 months ago

0.1.72

7 months ago

0.1.73

6 months ago

0.1.63

8 months ago

0.1.64

8 months ago

0.1.65

8 months ago

0.1.66

8 months ago

0.1.67

8 months ago

0.1.68

8 months ago

0.1.69

7 months ago

0.1.60

8 months ago

0.1.61

8 months ago

0.1.62

8 months ago

0.1.107

5 months ago

0.1.106

5 months ago

0.1.109

5 months ago

0.1.108

5 months ago

0.1.103

5 months ago

0.1.102

5 months ago

0.1.105

5 months ago

0.1.104

5 months ago

0.1.101

5 months ago

0.1.100

5 months ago

0.1.19

9 months ago

0.1.18

9 months ago

0.1.17

9 months ago

0.1.16

9 months ago

0.1.15

9 months ago

0.1.14

9 months ago

0.1.13

10 months ago

0.1.12

10 months ago

0.1.11

10 months ago

0.1.10

10 months ago

0.1.9

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago