|
| 1 | +# Testing URL Rewriting Features |
| 2 | + |
| 3 | +This document describes how to test the URL rewriting and enhanced middleware features in One. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +1. **Start the dev server:** |
| 8 | + ```bash |
| 9 | + cd apps/onestack.dev |
| 10 | + yarn dev |
| 11 | + ``` |
| 12 | + |
| 13 | +2. **Visit the test page:** |
| 14 | + Open http://localhost:6173/test-rewrites |
| 15 | + |
| 16 | +3. **Test subdomain routing:** |
| 17 | + Try these URLs in your browser (`.localhost` domains work on modern systems): |
| 18 | + - http://app1.localhost:6173 |
| 19 | + - http://app2.localhost:6173 |
| 20 | + - http://docs.localhost:6173 |
| 21 | + |
| 22 | +## Features to Test |
| 23 | + |
| 24 | +### 1. URL Rewriting |
| 25 | + |
| 26 | +The following rewrite rules are configured in `vite.config.ts`: |
| 27 | + |
| 28 | +```typescript |
| 29 | +{ |
| 30 | + web: { |
| 31 | + rewrites: { |
| 32 | + '*.localhost': '/subdomain/*', |
| 33 | + 'docs.localhost': '/docs', |
| 34 | + '/old-docs/*': '/docs/*' |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### 2. Middleware Capabilities |
| 41 | + |
| 42 | +The middleware in `app/_middleware.tsx` demonstrates: |
| 43 | + |
| 44 | +- **Request rewriting**: Modifying the URL before it reaches route handlers |
| 45 | +- **Response interception**: Returning responses directly from middleware |
| 46 | +- **Subdomain handling**: Detecting and routing based on subdomains |
| 47 | + |
| 48 | +### 3. Link Component Integration |
| 49 | + |
| 50 | +Links with internal paths like `/subdomain/app1` should automatically render with external URLs like `http://app1.localhost` when rewrites are configured. |
| 51 | + |
| 52 | +## Running Integration Tests |
| 53 | + |
| 54 | +```bash |
| 55 | +# Run all tests |
| 56 | +yarn test |
| 57 | + |
| 58 | +# Run rewrite tests specifically |
| 59 | +yarn vitest run rewrite-integration.test.tsx |
| 60 | + |
| 61 | +# Run with debugging (opens browser) |
| 62 | +DEBUG=1 yarn vitest run rewrite-integration.test.tsx |
| 63 | +``` |
| 64 | + |
| 65 | +## Test Files |
| 66 | + |
| 67 | +- **Test page**: `/app/test-rewrites.tsx` - Interactive test page |
| 68 | +- **Middleware**: `/app/_middleware.tsx` - Request/response handling |
| 69 | +- **Subdomain pages**: `/app/subdomain/[name]/index.tsx` - Dynamic subdomain routing |
| 70 | +- **Integration tests**: `/tests/rewrite-integration.test.tsx` - Automated tests |
| 71 | + |
| 72 | +## Manual Testing Checklist |
| 73 | + |
| 74 | +### Basic Functionality |
| 75 | + |
| 76 | +- [ ] Visit http://localhost:6173/test-rewrites |
| 77 | +- [ ] Check that current URL is displayed correctly |
| 78 | +- [ ] Verify links show their rendered hrefs |
| 79 | + |
| 80 | +### Subdomain Routing |
| 81 | + |
| 82 | +- [ ] Visit http://app1.localhost:6173 |
| 83 | +- [ ] Verify it shows "Subdomain: app1" |
| 84 | +- [ ] Navigate to http://app1.localhost:6173/about |
| 85 | +- [ ] Verify navigation between subdomain pages works |
| 86 | + |
| 87 | +### Link Transformation |
| 88 | + |
| 89 | +- [ ] Hover over subdomain links on test page |
| 90 | +- [ ] Verify browser shows subdomain URLs in status bar |
| 91 | +- [ ] Click subdomain links |
| 92 | +- [ ] Verify navigation works correctly |
| 93 | + |
| 94 | +### Middleware Response |
| 95 | + |
| 96 | +- [ ] Click "Test Middleware Response" button |
| 97 | +- [ ] Verify JSON response appears |
| 98 | +- [ ] Check response contains expected fields |
| 99 | + |
| 100 | +### Path Rewrites |
| 101 | + |
| 102 | +- [ ] Visit http://localhost:6173/old-docs/intro |
| 103 | +- [ ] Verify it redirects or rewrites to /docs/intro |
| 104 | + |
| 105 | +## Troubleshooting |
| 106 | + |
| 107 | +### Subdomain Not Resolving |
| 108 | + |
| 109 | +If `*.localhost` doesn't work on your system: |
| 110 | + |
| 111 | +1. **Check your OS**: Modern macOS and Linux support `.localhost` by default |
| 112 | +2. **Try 127.0.0.1**: Use `http://127.0.0.1:6173` instead |
| 113 | +3. **Add to hosts file** (if needed): |
| 114 | + ```bash |
| 115 | + sudo echo "127.0.0.1 app1.localhost" >> /etc/hosts |
| 116 | + sudo echo "127.0.0.1 app2.localhost" >> /etc/hosts |
| 117 | + ``` |
| 118 | + |
| 119 | +### Links Not Transforming |
| 120 | + |
| 121 | +1. Check that rewrites are configured in `vite.config.ts` |
| 122 | +2. Verify environment variable is set: `process.env.ONE_URL_REWRITES` |
| 123 | +3. Check browser console for errors |
| 124 | +4. Ensure you've rebuilt after config changes |
| 125 | + |
| 126 | +### Middleware Not Running |
| 127 | + |
| 128 | +1. Verify `_middleware.tsx` is in the correct location |
| 129 | +2. Check that it exports a default function |
| 130 | +3. Look for errors in server console |
| 131 | +4. Ensure middleware is created with `createMiddleware()` |
| 132 | + |
| 133 | +## Implementation Details |
| 134 | + |
| 135 | +### How It Works |
| 136 | + |
| 137 | +1. **Configuration**: Rewrite rules are defined in `vite.config.ts` |
| 138 | +2. **Environment**: Rules are passed to client via `process.env.ONE_URL_REWRITES` |
| 139 | +3. **Middleware**: Requests are modified before routing |
| 140 | +4. **Links**: The Link component applies reverse rewrites for display |
| 141 | +5. **Navigation**: React Navigation handles routing with rewritten paths |
| 142 | + |
| 143 | +### Key Files |
| 144 | + |
| 145 | +- `/packages/one/src/utils/rewrite.ts` - Rewrite utilities |
| 146 | +- `/packages/one/src/createMiddleware.ts` - Middleware types |
| 147 | +- `/packages/one/src/createHandleRequest.ts` - Request handling |
| 148 | +- `/packages/one/src/link/href.ts` - Link URL resolution |
| 149 | +- `/packages/one/src/router/getLinkingConfig.ts` - React Navigation integration |
0 commit comments